Function templates do implicit conversions for their arguments

TommiT tommitissari at hotmail.com
Thu Jul 4 06:55:16 PDT 2013


On Thursday, 4 July 2013 at 13:45:07 UTC, Maxim Fomin wrote:
> On Thursday, 4 July 2013 at 09:24:53 UTC, TommiT wrote:
>>
>> Yes, you are right in that the quote from TDPL refers to that 
>> specific function signature and is saying that the base type 
>> of T[] and T must match. But more generally, that quote is 
>> also saying that D doesn't try to do implicit conversion and 
>> type deduction at the same time for type-parameterized runtime 
>> arguments. In other words, the quote is saying: the type of 
>> the argument you pass to a templated function must match 
>> exactly to the templated type that the function template is 
>> expecting for that particular argument. That is: no implicit 
>> conversion happens to your variable before it is passed to the 
>> function as a runtime argument whose type is parameterized by 
>> the function.
>>
>>
>
> You heavily misunderstoond the topic.

No I didn't.

> The issue here is that in
>
> T[] find(T)(T[] haystack, T needle)
>
> type of neendle should correspond to base type of haystack. If 
> there is a contradiction ("at the same time implicit 
> conversions and type deduction"), dmd issues error.

Yes I understand this perfectly. No problem. This works exactly 
like it does in C++ and I know C++. Trust me.

> Actually if you pass integer static array, dmd deduces T to be 
> int, [..]

And that right there, "dmd deduces T to be int", is the crux of 
the matter. How on earth is DMD able to deduce T to be int, 
without using the implicit conversion from int[10] to int[] ?

DMD does the implicit conversion int[10] -> int[] while it is 
doing type deduction, which according to TDPL shouldn't happen.


More information about the Digitalmars-d mailing list