General topics | ||||||||||||||||
Flow control | ||||||||||||||||
Conditional execution statements | ||||||||||||||||
Iteration statements (loops) | ||||||||||||||||
Jump statements | ||||||||||||||||
Functions | ||||||||||||||||
Function declaration | ||||||||||||||||
Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
Exceptions | ||||||||||||||||
Namespaces | ||||||||||||||||
Types | ||||||||||||||||
Specifiers | ||||||||||||||||
| ||||||||||||||||
Storage duration specifiers | ||||||||||||||||
Initialization | ||||||||||||||||
Expressions | ||||||||||||||||
Alternative representations | ||||||||||||||||
Literals | ||||||||||||||||
Boolean -Integer -Floating-point | ||||||||||||||||
Character -String -nullptr(C++11) | ||||||||||||||||
User-defined(C++11) | ||||||||||||||||
Utilities | ||||||||||||||||
Attributes(C++11) | ||||||||||||||||
Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
Type alias declaration(C++11) | ||||||||||||||||
Casts | ||||||||||||||||
Memory allocation | ||||||||||||||||
Classes | ||||||||||||||||
Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
Special member functions | ||||||||||||||||
Templates | ||||||||||||||||
Miscellaneous | ||||||||||||||||
General | |||||||||||||||||||||
Literals | |||||||||||||||||||||
| |||||||||||||||||||||
Operators | |||||||||||||||||||||
Conversions | |||||||||||||||||||||
Contents |
" s-char-seq (optional)" | (1) | ||||||||
R" d-char-seq (optional)( r-char-seq (optional)) d-char-seq (optional)" | (2) | (since C++11) | |||||||
L" s-char-seq (optional)" | (3) | ||||||||
LR" d-char-seq (optional)( r-char-seq (optional)) d-char-seq (optional)" | (4) | (since C++11) | |||||||
u8" s-char-seq (optional)" | (5) | (since C++11) | |||||||
u8R" d-char-seq (optional)( r-char-seq (optional)) d-char-seq (optional)" | (6) | (since C++11) | |||||||
u" s-char-seq (optional)" | (7) | (since C++11) | |||||||
uR" d-char-seq (optional)( r-char-seq (optional)) d-char-seq (optional)" | (8) | (since C++11) | |||||||
U" s-char-seq (optional)" | (9) | (since C++11) | |||||||
UR" d-char-seq (optional)( r-char-seq (optional)) d-char-seq (optional)" | (10) | (since C++11) | |||||||
s-char-seq | - | A sequence of one or mores-char s |
s-char | - | One of
|
basic-s-char | - | A character from thebasic source character set(until C++23)translation character set(since C++23), except the double-quote", backslash\, or new-line character |
d-char-seq | - | A sequence of one or mored-char s, at most 16 characters long |
d-char | - | A character from thebasic source character set(until C++23)basic character set(since C++23), except parentheses, backslash andspaces |
r-char-seq | - | A sequence of one or morer-char s, except that it must not contain the closing sequence) d-char-seq" |
r-char | - | A character from thebasic source character set(until C++23)translation character set(since C++23) |
Syntax | Kind | Type | Encoding | ||||
---|---|---|---|---|---|---|---|
(1,2) | ordinary string literal | constchar[N] | ordinary literal encoding | ||||
(3,4) | wide string literal | constwchar_t[N] | wide literal encoding | ||||
(5,6) | UTF-8 string literal |
| UTF-8 | ||||
(7,8) | UTF-16 string literal | constchar16_t[N] | UTF-16 | ||||
(9,10) | UTF-32 string literal | constchar32_t[N] | UTF-32 |
In the types listed in the table above,N is the number of encoded code units, which is determinedbelow.
Ordinary and UTF-8(since C++11) string literals are collectively referred to as narrow string literals.
Evaluating a string literal results in a string literal object with staticstorage duration. Whether all string literals are stored innonoverlapping objects and whether successive evaluations of a string literal yield the same or a different object is unspecified.
The effect of attempting to modify a string literal object is undefined.
bool b="bar"==3+"foobar";// can be true or false, unspecified constchar* pc="Hello";char* p=const_cast<char*>(pc);p[0]='M';// undefined behavior
Raw string literalsRaw string literals are string literals with a prefix containing // OK: contains one backslash,// equivalent to "\\"R"(\)"; // OK: contains four \n pairs,// equivalent to "\\n\\n\\n\\n"R"(\n\n\n\n)"; // OK: contains one close-parenthesis, two double-quotes and one open-parenthesis,// equivalent to ")\"\"("R"-()""()-"; // OK: equivalent to "\n)\\\na\"\"\n"R"a()\a"")a"; // OK: equivalent to "x = \"\"\\y\"\""R"(x = ""\y"")"; // R"<<(-_-)>>"; // Error: begin and end delimiters do not match// R"-()-"-()-"; // Error: )-" appears in the middle and terminates the literal | (since C++11) |
String literal objects are initialized with the sequence of code unit values corresponding to the string literal’s sequence ofs-char s andr-char s(since C++11), plus a terminating null character (U+0000), in order as follows:
T
as the string literal’s array element type (see the tableabove):T
, then the escape sequence contributes a single code unit with valuev.T
, then the escape sequence contributes a single code unit with a unique value of typeT
, that is congruent tov mod 2ST
.Adjacent string literals are concatenated attranslation phase 6 (after preprocessing):
| (until C++11) | ||||
| (since C++11) |
"Hello, ""world!"// at phase 6, the 2 string literals form "Hello, world!" L"Δx = %"PRId16// at phase 4, PRId16 expands to "d"// at phase 6, L"Δx = %" and "d" form L"Δx = %d"
The following contexts expect a string literal, but do not evaluate it:
(since C++11) | |
| (since C++14) |
| (since C++20) |
(since C++26) |
It is unspecified whether non-ordinary string literals are allowed in these contexts, except that a literal operator name must use an ordinary string literal(since C++11). | (until C++26) |
Only ordinary string literals are allowed in these contexts. Eachuniversal character name and eachsimple escape sequence in an unevaluated string is replaced by the member of thetranslation character set it denotes. An unevaluated string that contains a numeric escape sequence or a conditional escape sequence is ill-formed. | (since C++26) |
String literals can be used toinitialize character arrays. If an array is initialized likechar str[]="foo";,str will contain a copy of the string"foo".
String literals are convertible and assignable to non-constchar* orwchar_t* in order to be compatible with C, where string literals are of typeschar[N] andwchar_t[N]. Such implicit conversion is deprecated. | (until C++11) |
String literals are not convertible or assignable to non-const | (since C++11) |
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.
constchar* p="abc\0def";// std::strlen(p) == 3, but the array has size 8
If a valid hexadecimal digit follows a hexadecimal escape sequence in a string literal, it would fail to compile as an invalid escape sequence. String concatenation can be used as a workaround:
//const char* p = "\xfff"; // error: hexadecimal escape sequence out of rangeconstchar* p="\xff""f";// OK: the literal is const char[3] holding {'\xff','f','\0'}
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_char8_t | 202207L | (C++23) (DR20) | char8_t compatibility and portability fix (allowinitialization of (unsigned)char arrays from UTF-8 string literals) |
__cpp_raw_strings | 200710L | (C++11) | Raw string literals |
__cpp_unicode_literals | 200710L | (C++11) | Unicode string literals |
#include <iostream> // array1 and array2 contains the same values:char array1[]="Foo""bar";char array2[]={'F','o','o','b','a','r','\0'}; constchar* s1= R"foo(Hello World)foo";// same asconstchar* s2="\nHello\n World\n";// same asconstchar* s3="\n""Hello\n"" World\n"; constwchar_t* s4= L"ABC" L"DEF";// OK, same asconstwchar_t* s5= L"ABCDEF";constchar32_t* s6= U"GHI""JKL";// OK, same asconstchar32_t* s7= U"GHIJKL";constchar16_t* s9="MN" u"OP""QR";// OK, same asconstchar16_t* sA= u"MNOPQR"; // const auto* sB = u"Mixed" U"Types";// before C++23 may or may not be supported by// the implementation; ill-formed since C++23 constwchar_t* sC= LR"--(STUV)--";// OK, raw string literal int main(){std::cout<< array1<<' '<< array2<<'\n'<< s1<< s2<< s3<<std::endl;std::wcout<< s4<<' '<< s5<<' '<< sC<<std::endl;}
Output:
Foobar Foobar Hello World Hello World Hello World ABCDEF ABCDEF STUV
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 411 (P2029R4) | C++98 | escape sequences in string literals were not allowed to map to multiple code units | allowed |
CWG 1656 (P2029R4) | C++98 | the characters denoted by numeric escape sequences in string literals were unclear | made clear |
CWG 1759 | C++11 | a UTF-8 string literal might have code units that are not representable inchar | char can represent all UTF-8 code units |
CWG 1823 | C++98 | whether string literals are distinct was implementation-defined | distinctness is unspecified, and same string literal can yield different object |
CWG 2333 (P2029R4) | C++11 | it was unclear whether numeric escape sequences were allowed in UTF-8/16/32 string literals | made clear |
CWG 2870 | C++11 | the concatenation result of two ordinary string literals was unclear | made clear |
P1854R4 | C++98 | ordinary and wide string literals with non-encodable characters were conditionally-supported | programs with such literals are ill-formed |
P2029R4 | C++98 | 1. it was unclear whether string literals could contain non-encodable characters 2. it was unclear whether string literals could contain numeric escape sequences such that the code units they represent are not representable in the literals' array element type | 1. made conditionally-supported for ordinary and wide string literals[1] 2. ill-formed if the code units are neither representable in the unsigned integer type corresponding to the underlying type |
user-defined literals(C++11) | literals with user-defined suffix[edit] |
C documentation forString literals |