Annoying thing about auto ref function template

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 22 01:31:08 PDT 2017


On Tuesday, March 21, 2017 09:27:55 Yuxuan Shui via Digitalmars-d wrote:
> On Tuesday, 21 March 2017 at 01:10:38 UTC, Jonathan M Davis wrote:
> > On Monday, March 20, 2017 22:14:37 Yuxuan Shui via
> >
> > Digitalmars-d wrote:
> >> On Monday, 20 March 2017 at 21:53:47 UTC, Jonathan M Davis
> >>
> >> wrote:
> >> > [...]
> >>
> >> Makes sense...
> >>
> >> OK, attempt 2: how about support implicit partial application
> >> for templates?
> >
> > Well, you can do something like
> >
> > template foo(T)
> > {
> >     auto foo()(auto ref T t)
> >     {
> >         return t;
> >     }
> > }
> >
> > void main()
> > {
> >     alias f = foo!int;
> >     auto a = f(42);
> > }
> >
> > and then foo!int, but you're not going to get a function
> > pointer out of it, since for that, you need to instantiate the
> > inner function template. It does give partial instantiation
> > though.
>
> This is looks doable. Can we do this for all auto ref functions
> in phobos?

Maybe, but I'm not sure that it's merited. On top of the fact that this
would make every auto ref function more verbose, remember that doing this
would add an extra template to every single auto ref function, which
increases their compilation overhead (as it is, we arguably instantiate
_way_ too many templates in many D programs). AFAIK, you're the first person
to ever bring this issue up, which implies that this is not something that
many folks are trying to do - and it _is_ fairly trivial to create wrappers
in the cases where you want to have an explicit instantiation. So, it might
make sense to do this, or it might just be unnecessary overhead for 99.99%
of cases while only mildly helping the .01% case. Certainly, it seems pretty
ugly to start saying that folks should be wrapping all auto ref functions in
an extra template. I'm inclined to think that we need a strong use case to
merit adding the extra overhead to all of these functions rather than having
the few who need something like this create wrapper functions.

So, what's your use case here? Why would you be looking to explicitly
instantiate templates (auto ref or otherwise) frequently enough that it's a
problem to just create a wrapper in the cases where you need to do it? Most
code does not explicitly instanitate templates, and there's usually no point
in doing so.

- Jonathan M Davis



More information about the Digitalmars-d mailing list