Home > front end >  Using prefix with string literal split over multiple lines
Using prefix with string literal split over multiple lines

Time:10-28

I have a Unicode string literal, let's say like this.

const char8_t x[] = u8"aaa\nbbb©\nccc\n";

I would like to split it over multiple lines for readability.

Which of the notations below are correct and equivalent to the one above?

const char8_t x[] =
        u8"aaa\n"
        u8"bbb©\n"
        u8"ccc\n";
const char8_t x[] =
        u8"aaa\n"
        "bbb©\n"
        "ccc\n";
const char8_t x[] =
        "aaa\n"
        u8"bbb©\n"
        "ccc\n";

All seem to compile but I'm afraid some of them may be implementation-defined.

CodePudding user response:

From this cppreference page, it would appear that all your code snippets are equivalent and well-defined:

Concatenation


If one of the strings has an encoding prefix and the other doesn't, the one that doesn't will be considered to have the same encoding prefix as the other.

Or, from this Draft C 17 Standard:

5.13.5 String literals      [lex.string]


13      In translation phase 6 (5.2), 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. …

  • Related