In my code base, I often initialize an array or vector if bytes use the following syntax:
uint16_t foo = 0xAB, bar = 0xCD
// bytes = { 0xA, 0xB, 0xC, 0xD }
std::array<uint8_t, 4> bytes = {{
foo >> 8,
foo & 0x00FF,
bar >> 8,
bar & 0x00FF
}};
I get the following error from clang ++:
error: non-constant-expression cannot
be narrowed from type 'int' to 'value_type' (aka 'unsigned char') in initializer list [-Wc++11-narrowing]
foo >> 8,
^~~~~~~~~~~~~
The compiler will prompt me to add static_cast to disable this error. I know casting will work, but I wonder if casting can be avoided and the syntax can be kept as elegant as it is?
Thank you for your help.
source
share