Implicit conversion better match than exact template?

Peter Alexander peter.alexander.au at gmail.com
Sun Feb 9 04:25:40 PST 2014


This writes "int" instead of "S". The implicit conversion is 
preferred over the exact match in the template. Is this correct? 
I can't find anything in the spec that explains how overload 
resolution works in the presence of templates.

I don't think a conversion should be preferred when an exact 
template match is available since there is potential for loss of 
information and unexpected results. It may also be explaining the 
inconsistencies in this bug: 
https://d.puremagic.com/issues/show_bug.cgi?id=9506


import std.stdio;

struct S
{
     int x;
     alias x this;
}

void f(T)(T t) { writeln(T.stringof); }
void f(int t)  { writeln(int.stringof); }

void main()
{
     f(S.init); // f(int) called
}


More information about the Digitalmars-d mailing list