3

Does code below is cpp standard??
I've seen it before and it worked well.
However, I'm not sure is this standard or not yet.

#include <iostream>

int main(){
    const char str[] = "a" "b";
    std::cout << str << std::endl;
    return 0;
}
jarmod
  • 71,565
  • 16
  • 115
  • 122
Ramlo Ban
  • 43
  • 4
  • The C++ standard *requires* that adjacent string literals are merged into one by an implementation. Implementations, if they claim compliance with the standard, must provide the guarantee. Bugs or flaws in the implementation may affect this. – Peter Jan 22 '21 at 14:34

1 Answers1

5

The preprocessor has a step that merges adjacent string literals. If the only thing sperating string literals is white space, then they get merged together into one combined string literal.


The full proceses is described in [lex.string]/7

In translation phase 6 ([lex.phases]), adjacent string-literals are concatenated. If both string-literals 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 3: This concatenation is an interpretation, not a conversion. Because the interpretation happens in translation phase 6 (after the string literal contents have been encoded in the string-literal's associated character encoding), a string-literal's initial rawness has no effect on the interpretation or well-formedness of the concatenation. — end note]

Table 13 has some examples of valid concatenations.
source     means  source     means  source     means
------------------------------------------------------
|u"a" u"b" u"ab" | U"a" U"b" U"ab" | L"a" L"b" L"ab" |
|u"a"  "b" u"ab" | U"a"  "b" U"ab" | L"a"  "b" L"ab" |
| "a" u"b" u"ab" | "a"  U"b" U"ab" | "a"  L"b" L"ab" |
------------------------------------------------------

Characters in concatenated strings are kept distinct. [Example 2: "\xA" "B" contains the two characters '\xA' and 'B' after concatenation (and not the single hexadecimal character '\xAB'). — end example]

NathanOliver
  • 171,901
  • 28
  • 288
  • 402