Function templates do implicit conversions for their arguments

Maxim Fomin maxim at maxim-fomin.ru
Wed Jul 3 21:52:12 PDT 2013


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.

> Now, here's a new example:
>
> void foo(T)(T[] slice) { }
>
> void main()
> {
>     int[10] arr;
>     foo(arr);
> }

and this template has single argument.

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


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


More information about the Digitalmars-d mailing list