Type deduction on templated constructor.

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 24 02:38:13 PDT 2014


francesco cattoglio:

> should this code compile? I understand that the literal "1" is 
> "int" therefore it can screw type deduction, but I wonder if 
> the compiler should be smart enough to deduce it correctly.

To keep both the compiler and programmers sane, D templates don't 
perform implicit type conversions. This sometimes is not handy, 
but on the whole saves from a large number of troubles.

So you can write (D V.2.066):

Vector!ubyte(ubyte(1), ubyte(1), ubyte(1));

Or you can create a little helper function that makes that code 
more DRY.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list