Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens with adjacent string literal concatenation when there is a modifier(L, u8, etc.)

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?
like image 768
Benoit Avatar asked Mar 03 '11 10:03

Benoit


People also ask

What is string literal concatenation?

Concatenate string literals in C/C++ A string literal is a sequence of characters, enclosed in double quotation marks (" ") , which is used to represent a null-terminated string in C/C++. If we try to concatenate two string literals using the + operator, it will fail.

What does C++ append to the end of a string literal constant?

Q. C++: What does C++ append to the end of a string literal constant? a null character string is a char array with a null value (0x00) after the last valid character in the string.

Are string literals null-terminated in C?

A string literal is not necessarily a null-terminated character sequence: if a string literal has embedded null characters, it represents an array which contains more than one string.

What is the type of a string literal in C++?

In C the type of a string literal is a char[]. In C++, an ordinary string literal has type 'array of n const char'. For example, The type of the string literal "Hello" is "array of 6 const char". It can, however, be converted to a const char* by array-to-pointer conversion.


2 Answers

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. ...

like image 166
Howard Hinnant Avatar answered Oct 05 '22 01:10

Howard Hinnant


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.

like image 39
ildjarn Avatar answered Oct 05 '22 00:10

ildjarn