문제

Compiling with -Wconversion,

uint32_val = 0x00000C00;
uint_8_val = ((uint32_val >> 8) & 0x000000FF);

gives no warning.

What compiler flag shall I use to get a warning?

도움이 되었습니까?

해결책

There is no warning because the expression:

((uint32_val >> 8) & 0x000000FF)

is always convertible to a type that fits in a byte. Your uint32_val is not cast, but takes part in a calculation. The compiler can optimize this, and in this case it is clear the result after bit-wise and-ing with 000000ff will always fit the provided l-value type, hence no warning.

다른 팁

Your code will compile fine. The type of plain constants is determined to be the best fitting.

You will get warning for this code:

uint32_val = 0x00000C00;
uint_8_val = ((uint32_val >> 8) & 0x000001FF);

Compilers shouldn't enforce coding guidelines. That's two different thing.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top