Template type deduction and specialization

Daniel Kozák via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 20 06:46:09 PDT 2015


DOC say  `may not have` not `must not have` ;-)

On Wed, 20 May 2015 13:24:22 +0000
Mike Parker via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
wrote:

> 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