Mar*_*ing 12 c c++ standards icc
我通常可以理解编译器警告背后的原因,但这个似乎是完全错误的.
#include <stdint.h>
uint8_t myfunc(uint8_t x,uint8_t y)
{
x |= y;
return x;
}
Run Code Online (Sandbox Code Playgroud)
带-Wall的intel编译器抱怨:
conversion from "int" to "uint8_t={unsigned char}" may lose significant bits
x |= y;
^
Run Code Online (Sandbox Code Playgroud)
这是正确的吗?上面的代码是否是非便携式和非标准的?
那是integer promotions在起作用.
在
x |= y;
Run Code Online (Sandbox Code Playgroud)
|运营商的两个操作数都被提升为int
x = (int)x | (int)y;
Run Code Online (Sandbox Code Playgroud)
然后结果转换回uint8_t失去精度.