www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Implicit conversion better match than exact template?

reply "Peter Alexander" <peter.alexander.au gmail.com> writes:
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
}
Feb 09 2014
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 02/09/2014 01:25 PM, Peter Alexander wrote:
 This writes "int" instead of "S". The implicit conversion is preferred
 over the exact match in the template. Is this correct?
No.
 I can't find  anything in the spec that explains how overload resolution
 works in the presence of templates.
 ...
TDPL states that if both non-templated function and templated function _match equally well_ the non-templated one will be preferred.
Feb 09 2014
parent "Peter Alexander" <peter.alexander.au gmail.com> writes:
On Sunday, 9 February 2014 at 12:31:34 UTC, Timon Gehr wrote:
 TDPL states that if both non-templated function and templated 
 function _match equally well_ the non-templated one will be 
 preferred.
Thanks. I always forget to check TDPL :-) https://d.puremagic.com/issues/show_bug.cgi?id=12117
Feb 09 2014