: in template specialization vs constraint

Shriramana Sharma via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Dec 22 07:29:16 PST 2015

import std.stdio;
void func(T)(T v) { writeln(1); }
void func(T: int)(T v) { writeln(2); }
void func(T)(T v) if (is(T: int)) { writeln(3); }
void main()
    ubyte s = 200;

The above code prints 2 twice. A fwe questions:

1) At func(100) why isn't the compiler complaining that it is able to match 
two templates i.e. the ones printing 2 and 3? Is it that since the second 
one is specialized but the third one apparently isn't, the compiler just 
ignores the third one?

2) How come func(s) doesn't invoke the template that prints 3? `T: int` in 
the context of specialization means an exact match but in the context of 
constraints it means implicitly convertible, no? The specialization section 
under http://dlang.org/spec/template.html is not very clear about this IMHO.

Shriramana Sharma, Penguin #395953

More information about the Digitalmars-d-learn mailing list