What happens with adjacent string literal concaten

2019-02-21 11:05发布

It is valid in C and C++ to break a string literal because the preprocessor or the compiler will concatenate adjacent string literals.

const char *zStr = "a" "b"; // valid

What happens when string literals are prefixed with L (wide characters), u (UTF-16), U (UTF-32), u8 (UTF-8), and raw string literals (R"foo(this is a "raw string literal" with double quotes)foo")?

For example, is the following allowed:

const wchar_t *zStr = L"a" "b"; // valid?

2条回答
Bombasti
2楼-- · 2019-02-21 12:03

In C++0x your example is valid according to [lex.string]/p13:

... If one string literal has no encoding-prefix, it is treated as a string literal of the same encoding-prefix as the other operand. ...

In C++03 this same section said that this code had undefined behavior:

... If a narrow string literal token is adjacent to a wide string literal token, the behavior is undefined. ...

查看更多
爷、活的狠高调
3楼-- · 2019-02-21 12:04

Yes, that particular example is allowed by C++0x. Any combination of prefixless and L-prefixed literals will be treated as though all are L-prefixed.

EDIT: Citation -- N3242 (current C++0x working draft) §2.14.5/13:

In translation phase 6 (2.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.

查看更多
登录 后发表回答