digitalmars.D.learn - Templates
- m (33/33) Aug 22 2009 Hello,
- Jarrett Billingsley (10/40) Aug 22 2009 It's because of an (absolutely silly) implicit conversion of char to
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?
best regards
m
Aug 22 2009
On Sat, Aug 22, 2009 at 12:11 PM, m<m 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.
Aug 22 2009








Jarrett Billingsley <jarrett.billingsley gmail.com>