www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Behavior of signed/unsigned conversion in template parameters

reply Peter Alexander <peter.alexander.au gmail.com> writes:
Just had this error crop up. I just wanted to check whether this is 
correct behavior as I don't know the details of D's templates. I'm using 
DMD 2.047 to compile with no flags.


struct A(uint N) { }
void   b(uint N)() { }
void   c(uint N)(A!(N) a) { }

void main()
{
   A!(1) a;  // OK
   b!(1)();  // OK
   c!(1)(f); // Error - cannot implicitly convert A!(1) to A!(N)
   c(a);     // Error - cannot implicitly convert A!(1) to A!(N)
}


Are those errors correct?

If so, why can I instantiate 'A' and 'b' with a signed int parameter, 
but can't instantiate 'c'?

It seems that the uint-ness of the template parameter is only enforced 
when there is an argument involved, but I don't understand the reasoning 
behind this (if any). I would expect either all of these to compile, or 
none to compile.

Thanks in advance.
Jul 30 2010
parent Peter Alexander <peter.alexander.au gmail.com> writes:
On 30/07/10 8:52 PM, Peter Alexander wrote:
 Just had this error crop up. I just wanted to check whether this is
 correct behavior as I don't know the details of D's templates. I'm using
 DMD 2.047 to compile with no flags.


 struct A(uint N) { }
 void b(uint N)() { }
 void c(uint N)(A!(N) a) { }

 void main()
 {
 A!(1) a; // OK
 b!(1)(); // OK
 c!(1)(f); // Error - cannot implicitly convert A!(1) to A!(N)
 c(a); // Error - cannot implicitly convert A!(1) to A!(N)
 }


 Are those errors correct?

 If so, why can I instantiate 'A' and 'b' with a signed int parameter,
 but can't instantiate 'c'?

 It seems that the uint-ness of the template parameter is only enforced
 when there is an argument involved, but I don't understand the reasoning
 behind this (if any). I would expect either all of these to compile, or
 none to compile.

 Thanks in advance.

Oops. If it wasn't obvious, the c!(1)(f) is meant to be c!(1)(a).
Jul 31 2010