Rule A2-13-2 (required, implementation, automated)
String literals with different encoding prefixes shall not be concatenated.
Rationale
Concatenation of wide and narrow string literals leads to undefined behavior. “In translation phase 6 (2.2), adjacent string-literals are concatenated. If both stringliterals have the same encoding-prefix, the resulting concatenated string literal has that encoding-prefix. If one string-literal has no encoding-prefix, it is treated as a string-literal of the same encoding-prefix as the other operand. If a UTF-8 string literal token is adjacent to a wide string literal token, the program is ill-formed. Any other concatenations are conditionally-supported with implementation-defined behavior. [ Note: This concatenation is an interpretation, not a conversion. Because the interpretation happens in translation phase 6 (after each character from a literal has been translated into a value from the appropriate character set), a string-literal’s initial rawness has no effect on the interpretation or well-formedness of the concatenation. -end note ]” [C++14 Language Standard] [3]
Example
//% $Id: A2-13-2.cpp 305629 2018-01-29 13:29:25Z piotr.serwa $
char16_t nArray[] =
u"Hello"
u"World"; // Compliant, "u" stands for char16_t type
char32_t nArray2[] =
U"Hello"
U"World"; // Compliant, "U" stands for char32_t type
wchar_t wArray[] =
L"Hello"
L"World"; // Compliant, "L" stands for wchar_t type - violates A2-13-3
// rule.
wchar_t mixed1[] =
"Hello"
L"World"; // Compliant
char32_t mixed2[] =
"Hello"
U"World"; // Compliant
char16_t mixed3[] =
"Hello"
u"World"; // Compliant
// wchar_t mixed1[] = u"Hello" L"World"; // Non-compliant - compilation error
// char32_t mixed2[] = u"Hello" U"World"; // Non-compliant - compilation error
See also
MISRA C++ 2008 [7]: required 2-13-5 Narrow and wide string literals shall not be concatenated.
HIC++ v4.0 [9]: 2.5.1 Do not concatenate strings with different encoding prefixes