Function templates do implicit conversions for their arguments

Maxim Fomin maxim at
Thu Jul 4 08:03:52 PDT 2013

On Thursday, 4 July 2013 at 13:55:17 UTC, TommiT wrote:
> 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.

There should be some conversion about how to make judgments based 
on arguments, otherwise loop of "No ..." can be indefinitely 
long. You argue that in code void foo(T)(T[] array) {} if static 
array is passed, there is violation of one quote from TDPL. You 
are wrong, because:

1) You incorrectly applied quote regarding code void foo(T)(T[] 
ar, T t){}  to a different situation. In this case t is tied to 
some type, in case we discussed, it doesn't.
2) In case of one parameter, a variable isn't tied to any type 
and usual implicit conversions are applied.

>> 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.

Style in which you are arguing is an argument to do not.

>> 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 is stupid, but not that. If it has T[] parameter, and int[10] 
static array which is convertible to int[] is passed, T is 
deduced to be int. What other types T can be? A float, object, or 
pointer to union?

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

This is flawed since you can:

int[10] arr;

Here there is no deduction puzzle, as parameter was passed 
explicitly and usual conversion was applied as in case of 
non-template function. And if you argue, that this should not 
happen, than you argue to make explicit "!int" typing which is 
absolutely redundant in this case.

More information about the Digitalmars-d mailing list