Template type deduction and specialization

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 20 21:48:35 PDT 2015


On Wednesday, May 20, 2015 19:20:19 Mike Parker via Digitalmars-d-learn wrote:
> On 5/20/2015 6:35 PM, Jonathan M Davis via Digitalmars-d-learn wrote:
>
> > I'm using a fairly recent version of dmd master, and it prints out the
> > address for px in both cases when I compile your code. So, if it's printing
> > out 100 for you on the second call, it would appear to be a bug that has
> > been fixed at some point since 2.067 (or whatever version you're using) was
> > released.
> >
> > - Jonathan M Davis
> >
> I'm using 2.067.0, but according to the section of the docs Daniel
> pointer me to[1], printing 100 is the correct behavior in the second call.
>
> [1] http://dlang.org/template.html#function-templates

Hmmm. It looks like when : is used directly in the template parameter list,
it doesn't mean the same thing as when it's used in an is expression. I
_never_ use : directly in the template parameter list, so I misunderstood
what it did. And looking over what it says, your printVal(T : T*) should be
used when explicitly calling printVal!(int*) but that printVal(px) will
print the address, because for whatever reason, IFTI doesn't work with the :
syntax directly in the template parameter list (I have no idea why, but that
section in the spec is pretty clear about that). So, the fact that it's
printing the address for me in both cases is bug. But if you're seeing it
print 100 for printVal!(int*)(px) and the address for printVal(px), then
that would be correct per the spec.

Personally, I wish that template specializations like this just didn't exist
in the language, because they're redundant with template constraints and
just add more complication to the language, but it's not like we're going to
get rid of them at this point, unfortunately. But because I think that
they're pointless, I never use them, and clearly am not familiar enough with
how they work.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list