Specialization - Major hole in the spec?

Peter Alexander peter.alexander.au at gmail.com
Thu Jan 5 13:36:54 PST 2012


On 5/01/12 3:11 PM, Timon Gehr wrote:
> On 01/05/2012 10:14 AM, Peter Alexander wrote:
>> On 5/01/12 1:53 AM, Timon Gehr wrote:
>>> On 01/05/2012 03:01 AM, Peter Alexander wrote:
>>>> On 5/01/12 1:19 AM, Timon Gehr wrote:
>>>>>> foo!int(0); // 1
>>>>>
>>>>> Matches both 1 and 2. 2 is strictly more specialized, therefore 2 is
>>>>> chosen. Mistake on your side.
>>>>
>>>> It chooses 1 because I have specified T to be int so 2 can't possibly
>>>> match.
>>>>
>>>
>>> Test it. int implicitly converts to float, therefore it matches 2.
>>>
>>> void foo(T)(T t){writeln(1);}
>>> void foo(T:float)(int t){writeln(2);}
>>> void main(){foo!int(0);} // prints "2"
>>
>> Strange, I could have sworn that printed 1 when I tried it the first
>> time.
>>
>> In any case, it is surely a bug. I have *explicitly* specified that T
>> must be int, yet it has called a version with T == float.
>
> No it has called a version with T : float. ":" means "implicitly
> converts to". This is by design.

Ah, that makes sense, although I'm not entirely sure I like the design.


More information about the Digitalmars-d mailing list