When I compile the following code with gcc :
int main()
{
unsigned char c = 1;
c <<= 1; // WARNING ON THIS LINE
return 0;
}
I get this warning :
conversion to ‘unsigned char’ from ‘int’ may alter its value [-Wconversion]
Why ? What is wrong with this code ? Actually, can I really use the oprator <<=
on a unsigned char
variable ?
Compilation command :
g++ test.cpp -Wconversion -o test.exe
This is a valid warning:
c <<= 1;
is equivalent to:
c = c << 1
and the rules for <<
say that the operands are promoted and in this case will be promoted to int
and the result is of the promoted type. So there will be a conversion from int
to unsigned char
at the end which may result in an altered value.
The code is valid, the warning is telling you that an implicit conversion is happening and in some cases the conversion could alter the value. Using a cast will silence the warning. Results of implicit conversions can be very counter-intuitive and in some cases undefined behavior. See the gcc Wconversion wiki for some details.
I don't see a way to remove the warning without expanding the operation out manually and using static_cast
:
c = static_cast<unsigned char>( c << 1 );
As we can see from the long thread on this gcc bug report not everyone feels that this is a useful case for this warning.
For reference from the draft C++ standard section 5.8
Shift operators:
The operands shall be of integral or unscoped enumeration type and integral promotions are performed. The type of the result is that of the promoted left operand [...]
and from section 5.17
Assignment and compound assignment operators:
The behavior of an expression of the form E1 op = E2 is equivalent to E1 = E1 op E2 except that E1 is evaluated only once. [...]
来源:https://stackoverflow.com/questions/29796479/wconversion-warning-while-using-operator-on-unsigned-char