I am doing embedded ARM programming with gcc 4.9. I've been using the -Wconversion switch because it's in my company's default dev tool configuration. I'm using the stdint.h types (uint8_t, uint32_t, etc).
The compiler creates warnings every time I perform a compound assignment or even simple addition. For example:
uint8_t u8 = 0;
uint16_t u16;
// These cause warnings:
u8 += 2;
u8 = u16 >> 8;
The "common method" to fix this is to use casts, as discussed here and here:
u8 = (uint8_t)(u8 + 2);
u8 = (uint8_t)(u16 >> 8);
In addition to this being ugly, I keep running into convincing evidence that casting is generally bad practice.
My questions:
- Why is it bad to use typecasts in this way?
- Do I lose anything by simply omitting
-Wconversionand letting the compiler do implicit conversions for me?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire