I am using VS 2005.
The following code generates a warning :
unsigned int ui = -1;
The following don't :
int a = -1;
unsigned int tempui = a;
int b = ui;
Could someone explain please/
Since the conversion from some (possibly signed) integer to an
unsigned integer is well-defined (*), each of the code fragments may
rely on this behaviour and intentionally use this implicit conversion
in which case there is no error. Obviously, the Microsoft Compiler
considers the chance of a programming error in the first example to be
higher than the chance of error in the second one. But the reason for
this is beyond me. In general, this kind of implicit conversion might
be unexpected in which case it would be a programming error. You'll
probably be able to shut up the compiler by writing
static_cast<unsigned>(-1) instead to make it explicit that you want
this conversion.
(* the resulting value will be congruent to the original modulo
pow(2,N) where N is the number of value bits in the target type. Note:
This only applies for conversions where the targe type is *unsigned*.
When the target type is *signed*, there is no such guarantee. I
believe it's undefined behaviour. It might be unspecified or
implementation-defined. Not sure about that without checking the
standard.)
Cheers!
SG