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;
}
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;
}
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]