Is the following implementation-defined:
signed char *cp = "\x96\xA0\xB4\xBE\xC8";
and as well as:
std::string = "\x96\xA0\xB4\xBE\xC8";
On systems with 8-bits wide signed char
, yes. A hex escape sequence in a narrow string literal has an implementation-defined value if it falls outside of the implementation-defined range defined for char
. Assuming 8-bit signed char
, any hex value greater than 7F
is outside the range of representable values.
Whether that literal is used to initialise a std::string
or a pointer to character is irrelevant in this regard.
You can use an array of unsigned char
instead of a string literal:
static constexpr unsigned char cp[] = {
0x96,
0xA0,
0xB4,
0xC8,
};
You can use this array to initialise a std::basic_string<unsigned char>
if you need it:
std::basic_string<unsigned char> s = {std::begin(cp), std::end(cp)};
P.S. Conversion from string literal to non-const char pointer is ill-formed (since C++11; prior the conversion was well-formed but deprecated).
P.P.S char
, unsigned char
and signed char
are always three distinct types whether char
is signed or not.