Templates
Jarrett Billingsley
jarrett.billingsley at gmail.com
Sat Aug 22 12:04:16 PDT 2009
On Sat, Aug 22, 2009 at 12:11 PM, m<m at m.m> wrote:
> Hello,
>
> i'm during reading of Learnt to Tango with D. In chapter 5 (about Templates) there are few samples of using templates. I've got problem with one of them.
>
> 1.
> template List( T )
> {
> pragma( msg, "List( T )" );
> }
> template List( T : int )
> {
> pragma( msg, "List( T : int )" );
> }
> template List( T : T[] )
> {
> pragma( msg, "List( T : T[] )" );
> }
> void main()
> {
> alias List!(char) A;
> alias List!(int) B;
> alias List!(char[]) C;
> }
>
> Should produce messages (according to what is said in book):
>
> List( T )
> List( T : int )
> List( T : T[] )
>
> but i've got:
>
> List( T : int )
> List( T : int )
> List( T : T[])
>
> Is there are any error, or what?
It's because of an (absolutely silly) implicit conversion of char to
int, and the way D does template specialization. Because char is
implicitly convertible to int, D sees that both List!(T) and List!(T:
int) as valid instantiations for 'char'. Then, it decides that since
List!(T: int) is more specialized, it should use it instead of
List!(T). You'll also get the same inane List!(T: int) specialization
if you try List!(bool), since bool is implicitly convertible to int
for some reason.
It's extremely frustrating.
More information about the Digitalmars-d-learn
mailing list