# String literal

> C++ Atlas: `cppatlas.com/w/cpp/language/string_literal` | Source: https://en.cppreference.com/w/cpp/language/string_literal | Domain: Language | Standards: C++11, C++14, C++20, C++23, C++26

In the types listed in the table above, N is the number of encoded code units, which is determined below.

## Executive Mental Model (Study Companion)

**Core Intuition:** A prvalue in C++17+ is not an object in memory—it is an unmaterialized recipe to initialize a target slot.

- **Standard Era:** C++11 → C++17 → C++23
- **Primary ISO Clauses:** [basic.lval], [conv.rval], [class.copy.elision], [dcl.type.decltype]
- **CppCon Talk Angle:** Show how C++17 reversed the arrow: previously prvalues were temporary objects we tried to elide away; now prvalues only become objects when materialization is forced.
- **Common Audience Trap:** Conflating an identifier's declared type (Widget&&) with its expression value category (lvalue inside the function body).
- **Stage Takeaway:** Never write return std::move(local); — it turns an implicit move-eligible lvalue into an explicit xvalue that blocks NRVO.

## Memory Map Walkthrough: C++17 Value Category Taxonomy & Materialization

C++17 classifies every expression along two orthogonal axes: glvalue (has identity, you can take its address or distinguish it from another object) and rvalue (eligible to be moved from). An lvalue has identity and cannot be moved from implicitly; a prvalue has no identity yet because it is an unmaterialized initialization recipe; an xvalue sits at the intersection of both—an expiring object that already occupies memory and can have its resources pilfered. Temporary materialization conversion converts a prvalue into an xvalue only at the exact boundary where an object identity is required, such as binding a reference or accessing a member.

### Identity vs. movability ([basic.lval] §7.2.1)

Forget 'left side vs right side of an assignment'. In modern C++, an expression is classified by two orthogonal questions: Does evaluation determine the identity of an existing object or function (glvalue)? And may its resources be reused because nothing else observes it afterward (rvalue)?

An lvalue has identity but is not movable. A prvalue ('pure rvalue') is an unmaterialized computation or initializer with no address of its own. An xvalue ('eXpiring value') is the overlap: an object that already exists in memory whose lifetime is about to end or was explicitly cast via std::move.

*Type vs. value category orthogonality:*
```cpp
void consume(Widget& w);   // binds lvalues
void consume(Widget&& w);  // binds rvalues (xvalue or prvalue)

void relay(Widget&& param) {
    // 'param' has type Widget&&, yet the expression 'param' is an LVALUE
    // because it has a name and an addressable location.
    consume(param);            // calls consume(Widget&)
    consume(std::move(param)); // xvalue -> calls consume(Widget&&)
}
```

**Key takeaway:** Named rvalue references are always lvalues inside expressions; only unnamed rvalue references (like std::move's return value) are xvalues.

### C++17 deferred temporary materialization ([conv.rval] & [class.copy.elision])

Prior to C++17, Widget w = Widget(42); conceptually created a temporary Widget, called the move constructor into w, and then allowed the compiler to elide that move—which still forced Widget's move constructor to be accessible.

C++17 fixed this at the grammar level. Widget(42) is a prvalue initialization recipe passed down to w's storage slot. No temporary object exists unless something forces temporary materialization ([conv.rval]), such as binding to const Widget& or reading .field on a temporary.

*Non-copyable, non-movable type returned by value:*
```cpp
struct Locked {
    int token;
    explicit Locked(int t) : token(t) {}
    Locked(const Locked&) = delete;
    Locked(Locked&&) = delete;
};

Locked make_locked() {
    return Locked{99}; // Legal since C++17: initializes caller slot directly
}

Locked x = make_locked(); // Zero moves, zero copies required
```

**Key takeaway:** Prvalue chains (make() -> make() -> variable) collapse into a single in-place construction at the final destination.

## Official Specification Summary (cppreference)

### Syntax

- `"`*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)

### Explanation

- `s-char-seq` - A sequence of one or more *s-char* s
- `s-char` - One of

  - a *basic-s-char*
  - an escape sequence, as defined in escape sequences
  - a universal character name, as defined in escape sequences
- `basic-s-char` - A character from the translation character set, except the double-quote `"`, backslash `\`, or new-line character
- `d-char-seq` - A sequence of one or more *d-char* s, at most 16 characters long
- `d-char` - A character from the basic character set, except parentheses, backslash and spaces
- `r-char-seq` - A sequence of one or more *r-char* s, except that it must not contain the closing sequence `)d-char-seq"`
- `r-char` - A character from the translation character set

**Syntax**: (1,2)

**Kind**: ordinary string literal

**Type**: `const char[N]`

**Encoding**: ordinary literal encoding

**Syntax**: (3,4)

**Kind**: wide string literal

**Type**: `const wchar_t[N]`

**Encoding**: wide literal encoding

**Syntax**: (5,6)

**Kind**: UTF-8 string literal

**Type**: `const char[N]` (until C++20), `const char8_t[N]` (since C++20)

**Encoding**: UTF-8

**Syntax**: (7,8)

**Kind**: UTF-16 string literal

**Type**: `const char16_t[N]`

**Encoding**: UTF-16

**Syntax**: (9,10)

**Kind**: UTF-32 string literal

**Type**: `const char32_t[N]`

**Encoding**: UTF-32

In the types listed in the table above, `N` is the number of encoded code units, which is determined below.

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 static storage duration. Whether all string literals are stored in nonoverlapping 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.

```cpp
bool b = "bar" == 3 + "foobar"; // can be true or false, unspecified

const char* pc = "Hello";
char* p = const_cast<char*>(pc);
p[0] = 'M'; // undefined behavior
```

(since C++11):

### Raw string literals

Raw string literals are string literals with a prefix containing `R` (syntaxes (2,4,6,8,10)). They do not escape any character, which means anything between the delimiters `d-char-seq (` and `)d-char-seq` becomes part of the string. The terminating *d-char-seq* is the same sequence of characters as the initial *d-char-seq*.

```cpp
// 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
```

### Initialization

String literal objects are initialized with the sequence of code unit values corresponding to the string literal’s sequence of *s-char* s and *r-char* s (since C++11), plus a terminating null character (U+0000), in order as follows:

1) For each contiguous sequence of *basic-s-char* s, *r-char* s, (since C++11) simple escape sequences and universal character names, the sequence of character it denotes is encoded to a code unit sequence using the string literal’s associated character encoding. If a character lacks representation in the associated character encoding, then the program is ill-formed.

If the associated character encoding is stateful, the first such sequence is encoded beginning with the initial encoding state and each subsequent sequence is encoded beginning with the final encoding state of the prior sequence.

2) For each numeric escape sequence, given `v` as the integer value represented by the octal or hexadecimal number comprising the sequence of digits in the escape sequence, and `T` as the string literal’s array element type (see the table above):

   - If `v` does not exceed the range of representable values of `T`, then the escape sequence contributes a single code unit with value `v`.
   - Otherwise, if the string literal is of syntax (1) or (3), and (since C++11) `v` does not exceed the range of representable values of the corresponding unsigned type for the underlying type of `T`, then the escape sequence contributes a single code unit with a unique value of type `T`, that is congruent to v mod 2^(S), where `S` is the width of `T`.
   - Otherwise, the program is ill-formed.

If the associated character encoding is stateful, all such sequences have no effect on encoding state.

3) Each conditional escape sequence contributes an implementation-defined code unit sequence.

If the associated character encoding is stateful, it is implementation-defined what effect these sequences have on encoding state.

### Concatenation

Adjacent string literals are concatenated at translation phase 6 (after preprocessing):

- If the two string literals are of the same kind, the concatenated string literal is also of that kind.

(until C++11):

- If an ordinary string literal is adjacent to a wide string literal, the behavior is undefined.

(since C++11):

- If an ordinary string literal is adjacent to a non-ordinary string literal, the concatenated string literal is of the kind of the latter.
- If a UTF-8 string literal is adjacent to a wide string literal, the program is ill-formed.

(until C++23):

- Any other combination is conditionally supported with implementation-defined semantics.[1]

(since C++23):

- Any other combination is ill-formed.

```cpp
"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"
```

1. ↑ No known implementation supports such concatenation.

### Unevaluated strings

The following contexts expect a string literal, but do not evaluate it:

- language linkage specification

(since C++11):

- `static_assert`
- literal operator name

(since C++14):

- `[[deprecated]]`

(since C++20):

- `[[nodiscard]]`

(since C++26):

- deleted function body

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)

(since C++26):

Only ordinary string literals are allowed in these contexts.

Each universal character name and each simple escape sequence in an unevaluated string is replaced by the member of the translation character set it denotes. An unevaluated string that contains a numeric escape sequence or a conditional escape sequence is ill-formed.

### Notes

String literals can be used to initialize character arrays. If an array is initialized like `char str[] = "foo";`, `str` will contain a copy of the string `"foo"`.

String literals are convertible and assignable to non-const `char*` or `wchar_t*` in order to be compatible with C, where string literals are of types `char[N]` and `wchar_t[N]`. Such implicit conversion is deprecated. (until C++11)

String literals are not convertible or assignable to non-const `CharT*`. An explicit cast (e.g. `const_cast`) must be used if such conversion is wanted. (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.

```cpp
const char* 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:

```cpp
//const char* p = "\xfff"; // error: hexadecimal escape sequence out of range
const char* 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 (allow initialization 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 |

### Example

```cpp
#include <iostream>

// array1 and array2 contains the same values:
char array1[] = "Foo" "bar";
char array2[] = {'F', 'o', 'o', 'b', 'a', 'r', '\0'};

const char* s1 = R"foo(
Hello
  World
)foo";
// same as
const char* s2 = "\nHello\n  World\n";
// same as
const char* s3 = "\n"
                 "Hello\n"
                 "  World\n";

const wchar_t* s4 = L"ABC" L"DEF"; // OK, same as
const wchar_t* s5 = L"ABCDEF";
const char32_t* s6 = U"GHI" "JKL"; // OK, same as
const char32_t* s7 = U"GHIJKL";
const char16_t* s9 = "MN" u"OP" "QR"; // OK, same as
const char16_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

const wchar_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:

```text
Foobar Foobar

Hello
  World

Hello
  World

Hello
  World

ABCDEF ABCDEF STUV
```

### Defect reports

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 in `char` | `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 |

1. ↑ P1854R4 was accepted as a DR later, overriding this resolution.

### References

- C++23 standard (ISO/IEC 14882:2024):

  - 5.13.5 String literals [lex.string]

- C++20 standard (ISO/IEC 14882:2020):

  - 5.13.5 String literals [lex.string]

- C++17 standard (ISO/IEC 14882:2017):

  - 5.13.5 String literals [lex.string]

- C++14 standard (ISO/IEC 14882:2014):

  - 2.14.5 String literals [lex.string]

- C++11 standard (ISO/IEC 14882:2011):

  - 2.14.5 String literals [lex.string]

- C++03 standard (ISO/IEC 14882:2003):

  - 2.13.4 String literals [lex.string]

- C++98 standard (ISO/IEC 14882:1998):

  - 2.13.4 String literals [lex.string]

### See also

- [user-defined literals (C++11)](https://en.cppreference.com/w/cpp/language/user_literal): literals with user-defined suffix
- [C documentation for String literals](https://en.cppreference.com/w/c/language/string_literal)

## Podcast Host Debate & Socratic Standard Traps

Use the prompts and tricky ISO standard questions below to guide a Socratic audio discussion:

- Debate why teaching 'lvalue means left-hand side of =' actively breaks programmers once const variables and std::move enter the picture.
- Walk through how C++17 deferred materialization makes non-movable types like std::atomic<int> returnable by value.
- Quiz each other on why "hello" is an lvalue and why return std::move(local) is a classic performance pessimization.

### Trap 1: [lex.string] §5.13.5 (standard-lawyer)

What is the value category and exact type of the string literal expression "cppcon"?

```cpp
auto p = &"cppcon";
```

A. prvalue of type const char*
B. lvalue of type const char[7]
C. prvalue of type const char[7]
D. xvalue of type const char(&)[7]

**Standard Answer (B):** lvalue of type const char[7]

**Why:** Unlike numeric literals such as 42 (which are prvalues), string literals have static storage duration in read-only memory. They are lvalues of array type const char[N] including the null terminator, which is why taking &"cppcon" compiles cleanly.

*CppCon speaker note:* Great opening warm-up slide at CppCon: 80% of the room picks prvalue by analogy with integer literals.

### Trap 2: [dcl.type.decltype] §9.2.9.6 (subtle)

Given int x = 10;, what are the deduced types of a and b below?

```cpp
int x = 10;
decltype(x)   a = x;
decltype((x)) b = x;
```

A. a is int, b is int
B. a is int&, b is int&
C. a is int, b is int&
D. a is int, b is int&&

**Standard Answer (C):** a is int, b is int&

**Why:** decltype has two rules: on an unparenthesized id-expression (x), it yields the declared type (int). Wrapping parentheses turns (x) into a general expression, where decltype yields T& for lvalues, T&& for xvalues, and T for prvalues.

### Trap 3: [class.copy.elision] §11.9.6 (cppcon-stage)

In C++17 and later, what happens when returning a named local variable via return std::move(buf);?

```cpp
std::string build() {
    std::string buf = "long heap string...";
    return std::move(buf);
}
```

A. Faster than return buf; because it forces a move instead of a copy
B. Prevents NRVO and forces a move construction instead of zero-cost in-place construction
C. Undefined behavior because buf is destroyed before the move finishes
D. Triggers guaranteed C++17 copy elision

**Standard Answer (B):** Prevents NRVO and forces a move construction instead of zero-cost in-place construction

**Why:** NRVO (Named Return Value Optimization) only applies when the return operand is the plain name of an automatic local variable. Wrapping it in std::move(buf) changes the expression to a function call yielding an xvalue, blocking NRVO and forcing an actual move constructor call (-Wpessimizing-move).

*CppCon speaker note:* Emphasize: plain return buf; already moves implicitly if NRVO doesn't happen, so std::move here can only hurt.

### Trap 4: [expr.ref] §7.6.1.5 (standard-lawyer)

Given struct S { int m; }; and a function S make_s();, what is the value category of make_s().m?

```cpp
struct S { int m; };
S make_s();
// What is make_s().m ?
```

A. lvalue
B. prvalue
C. xvalue
D. Ill-formed because make_s() is unmaterialized

**Standard Answer (C):** xvalue

**Why:** Member access E1.E2 on a prvalue E1 first triggers temporary materialization ([conv.rval]), turning make_s() into a temporary xvalue. A non-static data member access on an rvalue yields an xvalue designating that subobject.

### Trap 5: [expr.pre.incr] & [expr.post.incr] (subtle)

For built-in int i = 0;, which expression is well-formed C++?

```cpp
int i = 0;
```

A. &(i++)
B. (i++) = 5
C. ++ ++i
D. int& r = i++;

**Standard Answer (C):** ++ ++i

**Why:** Built-in prefix ++i modifies i and yields an lvalue referring to i, so chaining ++ ++i mutates i twice validly. Built-in postfix i++ produces a prvalue copy of the old value, which cannot have its address taken, be assigned to, or bind to a non-const lvalue ref.

---
Attribution: content from https://en.cppreference.com/w/cpp/language/string_literal (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.