Boost logo

Boost :

Subject: Re: [boost] [mpl] clang error for narrowing from unsigned int 0xffffffff to long
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2013-11-14 00:17:01


On 14/11/2013 17:22, Quoth Edward Diener:
> Or my own preference of 'static_cast<long>(0xffffffff)'. But using
> '0xffffffffL' does not work. I admit that understanding the C++
> Standard's different interprtetation of these two forms is beyond me.

0xffffffffL is an invalid literal value because it is out of range for
the "long" type. (Using UL or even just U would make it valid, but
still not the type you wanted.)

0xffffffff does not explicitly specify a size so the compiler is allowed
to pick one itself; it will probably infer "unsigned int", as "signed
int" is too small. (Or on platforms where "int" is 16-bit, it would
infer "unsigned long".)

An explicit cast on an integer is allowed to reinterpret unsigned
to/from signed and/or extend or truncate bits, through long historic
usage in C code.

(Technically that usage should ideally require reinterpret_cast instead
of static_cast, since you're reinterpreting the sign bit. But again I
think tradition won out here -- and it's a less scary conversion than
other places you need reinterpret_cast.)


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk