Function templates do implicit conversions for their arguments

TommiT tommitissari at hotmail.com
Thu Jul 4 02:24:52 PDT 2013


On Thursday, 4 July 2013 at 04:52:13 UTC, Maxim Fomin wrote:
> On Wednesday, 3 July 2013 at 20:33:26 UTC, TommiT wrote:
>> Apparently I'm correct with my initial assertion after all, 
>> but only as it relates to implicit conversion from static to 
>> dynamic arrays. Let me start this thread again by using my 
>> initial opening statement:
>>
>> This is a pretty big delta between C++ and D. It's going to 
>> surprise everybody coming from C++, especially when it says in 
>> TDPL (page 140) that: "However, having the language attempt
>> combinatorially at the same time implicit conversions and type 
>> deduction is a dicey proposition in the general case, so D 
>> does not attempt to do all that".
>
> "at the same time implicit conversions and type deduction"
>
> Quoted comment from TDPL is related to template:
>
> T[] find(T)(T[] haystack, T needle)
>
> which means that base type of T[] and T must match.

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.


On Thursday, 4 July 2013 at 04:52:13 UTC, Maxim Fomin wrote:
> On Wednesday, 3 July 2013 at 20:33:26 UTC, TommiT wrote:
>> Now, here's a new example:
>>
>> void foo(T)(T[] slice) { }
>>
>> void main()
>> {
>>    int[10] arr;
>>    foo(arr);
>> }
>
> and this template has single argument.

But that's not relevant as I explained above.


On Thursday, 4 July 2013 at 04:52:13 UTC, Maxim Fomin wrote:
> On Wednesday, 3 July 2013 at 20:33:26 UTC, TommiT wrote:
>> See what I mean? int[10] is a distinct type from int[], so an 
>> implicit conversion must happen before 'arr' is passed to 
>> 'foo'. Implicit conversions never happen for arguments passed 
>> to templated functions in C++ (and neither in D according to 
>> that quote from TDPL above).
>
> Surprise, following implicit conversions are happen all over 
> the D.
> - pointer type to void type;
> - derived class to base class;
> - static array to dynamic array;
> - class implementator to interface;
> - enum to base type;
> - base type to aliased type;
> - ... and much more.

Okay, my wording was not the best possible there. I can see that 
what I wrote could be misinterpreted. I was _not_ saying that C++ 
doesn't do implicit conversions all over the place (or that D 
doesn't). Actually, C++ probably does more implicit conversions 
than D does. I was just saying that implicit conversion never 
happens in C++ for a variable that is passed to a function as an 
argument whose type is fully or partly parameterized by that 
function. And this is a big difference between the two languages 
which should be emphasised somewhere in the documentation and 
that quote should be fixed in the next edition of TDPL.


On Thursday, 4 July 2013 at 04:52:13 UTC, Maxim Fomin wrote:
> On Wednesday, 3 July 2013 at 20:33:26 UTC, TommiT wrote:
>> And I'll just finish with my initial closing argument:
>> This difference between D and C++ should be noted somewhere in 
>> the documentation with big red letters.
>
> Initial closing argument is based on incorrect understanding of 
> the quote. Instead of comparing C++ and D it is better to read 
> carefully spec and TDPL.

Perhaps Andrei could tell us what that quote means. Or perhaps 
you could show me the part of the spec which I should have read 
more carefully.


More information about the Digitalmars-d mailing list