Many methods take char
to be the ‘byte type’*. However, it is unclear (to me) whether it is implementation defined behaviour what a cast from an unsigned char
to a char
ought to do when the unsigned char
‘s value is greater than CHAR_MAX
.
This section is from C++11(ISO/IEC 14882:2011) §4.7 Integral conversions [conv.integral/2]
If the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined.
This section of the standard is also mentioned in a similar question, but no mention is given to how we can preserve the bit-pattern for these conversions.
So, how should I cast a uint8_t
, unsigned char
, or other char-width type to-and-fro a char
, preserving the bit pattern?
Note: This question is about C++ in general, not a specific version. If something was unacceptable in C++11, but works in C++14, then it would be useful if the answer could contrast the two versions.
*for example, x64 SIMD intrinsics such as __m128i _mm_set1_epi8(char)
use char
.