prefixed string literals concatenated with prefixed ones?
In commentary on this answer, a dispute arose over whether a conforming implementation of C2011 is required, during translation, to perform string concatenation of un-prefixed string literals with adjacent prefixed string literals. Examples:
char16_t ustring[] = u"Unicode" " string";
wchar_t wstring[] = "Wide " L"string";
C99 did specify that such concatenation takes place. C++2011 is also pretty clear on the topic. The relevant provision of C2011 has different and more restrictive wording than either of those, however:
In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and identically-prefixed string literal tokens are concatenated into a single multibyte character sequence. [...]
(C2011, 6.4.5/5; emphasis added)
Surely a prefixed string literal and an unprefixed one are not "identically prefixed", right?
On the other hand, that paragraph continues,
[...] If any of the tokens has an encoding prefix, the resulting multibyte character sequence is treated as having the same prefix; otherwise, it is treated as a character string literal. Whether differently-prefixed wide string literal tokens can be concatenated and, if so, the treatment of the resulting multibyte character sequence are implementation-defined.
The first part of that seems to be speaking to the case in question, but it also seems to be dependent on the concatenation being performed in the first place, without specifying that it must be performed. Perhaps the last part is meant to permit that as implementation-defined behavior, but it doesn't quite fit, because although prefixed and unprefixed literals are certainly "differently-prefixed", they are not both wide.
Certainly it seems that such concatenation as I describe is intended to be performed, for examples are presented in paragraph 6.4.5/9. But as all language lawyers know, the examples are non-normative. Is there a plausible way to interpret the normative text to require conforming implementations to perform this concatenation? Or should lack of the same perhaps be considered a defect in the standard? Is it implementation defined whether such concatentations are performed?
p2 from the same subclause:
A character string literal is a sequence of zero or more multibyte characters enclosed in double-quotes, as in "xyz"
.
Read the quote again, emphasis mine:
In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and identically-prefixed string literal tokens are concatenated into a single multibyte character sequence.
You can concatenate character string literal tokens and identically-prefixed string literal tokens that are adjacent.
(To avoid any doubt, there's no "character literal" in C; 'c'
is a character constant, see §6.4.4.4.)
上一篇: 为什么在.cpp中包含<.hpp>,而不是在.hpp中包含<.cpp>?
下一篇: 前缀字符串文字与前缀连接?