Function templates do implicit conversions for their arguments

TommiT tommitissari at hotmail.com
Thu Jul 4 03:03:23 PDT 2013


On Thursday, 4 July 2013 at 09:24:53 UTC, TommiT wrote:
> [..] Perhaps Andrei could tell us what that quote means. [..]

No need for that. I'll explain what that quote means.

Here's that quote again:
"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."

Here's a simpler paraphrasing of that quote:
"D doesn't attempt to do implicit conversion and type deduction 
at the same time."

In this context, "type deduction" means the process of trying to 
figure out what kind of a signature a function template should 
instantiate to, in other words, what the template parameters of 
the instantiated function should be.

For example, given a function template:

void foo(T)(Array!T a) { }

...and a call which forces that function template to instantiate:

Array!int arr;
foo(arr); // OK

Type deduction is able to figure out that T must be int, and 
instantiates the function template to:
void foo(Array!int a) { }

But, if you try to pass to foo a variable of some user defined 
type MyType which merely implicitly converts Array!int, then the 
type deduction fails, because "D doesn't attempt to do implicit 
conversion and type deduction at the same time".

MyType mt;
foo(mt); // Error

Now, we can't test this because D doesn't have an implicit 
conversion operator (like C++ does), but it could have it in the 
future, and that's not really even relevant.


More information about the Digitalmars-d mailing list