www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Weird integral promotion issue (?)

reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
Hi,

alexrp alexrp ~/Projects/tests $ dmd test.d
test.d(9): Error: cannot implicitly convert expression (240) of type int 
to byte
alexrp alexrp ~/Projects/tests $ cat test.d
import std.stdio, std.traits;

void foo(T)(T val) if (isIntegral!T)
{
}

void main()
{
     foo!byte(0b11110000);
}

Why? I explicitly told the compiler that T == byte. Passing 0b00001111 
works perfectly fine.

-- 
- Alex
May 12 2012
parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 13.05.2012 1:08, Alex Rønne Petersen wrote:
 Hi,

 alexrp alexrp ~/Projects/tests $ dmd test.d
 test.d(9): Error: cannot implicitly convert expression (240) of type int
 to byte
 alexrp alexrp ~/Projects/tests $ cat test.d
 import std.stdio, std.traits;

 void foo(T)(T val) if (isIntegral!T)
 {
 }

 void main()
 {
 foo!byte(0b11110000);
 }

 Why? I explicitly told the compiler that T == byte. Passing 0b00001111
 works perfectly fine.

byte is in range of -128..127 use ubyte Gotcha! -- Dmitry Olshansky
May 12 2012