Template type deduction and specialization

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 20 06:24:22 PDT 2015


On Wednesday, 20 May 2015 at 09:35:43 UTC, Daniel Kozak wrote:

> DOCS: http://dlang.org/template.html#function-templates
> says: Function template type parameters that are to be 
> implicitly deduced may not have specializations:

OK, having reread this, I'm not clear at all what's going on. 
Here, I'm instantiating the templates such that the types are 
implicitly deduced from the function arguments and they pick up 
the specializations just fine.

```
T sum(T : ulong)(T lhs, T rhs) {
     writeln("Integrals");
     return cast(T)(lhs + rhs);
}

T sum(T : real)(T lhs, T rhs) {
     writeln("Floating Point");
     import std.math : round;
     return round(lhs + rhs);
}

void main() {
     writeln(sum(10L, 20L));
     writeln(sum(10.11, 3.22));
}
```

If the documentation is correct, then this shouldn't work, but it 
does. It breaks only when specializing on pointers and arrays, in 
which case I have to implicitly instantiate.


More information about the Digitalmars-d-learn mailing list