使用带字符串文字的前缀拆分多行
Using prefix with string literal split over multiple lines
我有一个 Unicode 字符串文字,让我们这样说吧。
const char8_t x[] = u8"aaa\nbbb©\nccc\n";
为了便于阅读,我想将它分成多行。
下面哪些符号是正确的并且等同于上面的符号?
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";
似乎都可以编译,但恐怕其中一些可能是实现定义的。
从 this cppreference page 开始,您的所有代码片段似乎都是等效且定义明确的:
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.
或者,来自 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. …
您可以在此处使用原始字符串文字:
const char8_t x[] =
u8R"(aaa
bbb©
ccc
)";
您不需要明确声明换行符。保留缩进。
这是一个缩进的例子:
const char8_t x[] =
u8R"(SomeRecord
Name: $NAME
Date: $DATE
)";
我有一个 Unicode 字符串文字,让我们这样说吧。
const char8_t x[] = u8"aaa\nbbb©\nccc\n";
为了便于阅读,我想将它分成多行。
下面哪些符号是正确的并且等同于上面的符号?
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";
似乎都可以编译,但恐怕其中一些可能是实现定义的。
从 this cppreference page 开始,您的所有代码片段似乎都是等效且定义明确的:
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.
或者,来自 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. …
您可以在此处使用原始字符串文字:
const char8_t x[] =
u8R"(aaa
bbb©
ccc
)";
您不需要明确声明换行符。保留缩进。 这是一个缩进的例子:
const char8_t x[] =
u8R"(SomeRecord
Name: $NAME
Date: $DATE
)";