How to specify a template that uses unqualified type, like any normal function

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 14 15:27:39 PDT 2017


On Monday, August 14, 2017 17:43:44 Dominikus Dittes Scherkl via 
Digitalmars-d-learn wrote:
> On Monday, 14 August 2017 at 15:20:28 UTC, Steven Schveighoffer
>
> wrote:
> > On 8/14/17 9:48 AM, Dominikus Dittes Scherkl wrote:
> > > uint foo(T)(Unqual!T n) // first try
> > > {
> > >
> > >     ++n; // modify should be possible
> > >     return 42;
> > >
> > > }
> > > Any ideas what I need to do to make this work?
> >
> > This isn't exactly supported. Implicit Function Template
> > Instantiation (IFTI) will deduce the parameters to be the types
> > that you pass in. You can't deduce them and then change the
> > parameter types. This is a limitation of IFTI that I have
> > struggled with in the past.
>
> A little unfortunate, because I would consider this the standard
> usecase.
> You only overload functions if they do something special with
> const or shared or immutable parameters, but for templates you
> get a different implementation for these all the time, and you
> didn't even have a chance to avoid that useless code-bloat? What
> a pitty.

As I understand it, sufficiently smart C++ compilers will figure out that
the code for two template instantiations can be the same even if the types
aren't, and they'll merge the definitions in the generated code, so you only
really get one. And that's the typical solution for this particular problem.
Unfortunately, dmd doesn't have that yet. I don't know what ldc's backend is
able to do on this front, but I suspect that for this to fully work, the
frontend has to be doing it, in which case, ldc will have it when dmd has
it. The other major, related problem is that normally, all of the templates
that get instantiated end up in the final code even if that makes no sense
(most notably, stuff like isInputRange). So, there's definitely work to be
done towards reducing template bloat in the D compilers.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list