# Erroneous behaviour and C++26 language changes

> C++26 adds erroneous behaviour, a new category for code that is wrong but still well-defined, and uses it to take uninitialized reads out of undefined behaviour. Alongside it come several rules that turn old traps into compile errors, plus the small conveniences `_`, `= delete("why")`, computed `static_assert` messages, and `#embed`.

- **Source:** C++ Atlas (https://cppatlas.com/topics/erroneous-behaviour-and-cpp26-language-changes)
- **Track:** C++26
- **Estimated reading time:** 15 minutes
- **ISO C++ clauses:** [defns.erroneous] (https://eel.is/c++draft/defns.erroneous), [intro.abstract] (https://eel.is/c++draft/intro.abstract), [basic.indet] (https://eel.is/c++draft/basic.indet), [dcl.attr.indet] (https://eel.is/c++draft/dcl.attr.indet), [stmt.return] (https://eel.is/c++draft/stmt.return), [expr.delete] (https://eel.is/c++draft/expr.delete), [expr.arith.conv] (https://eel.is/c++draft/expr.arith.conv), [basic.scope.scope] (https://eel.is/c++draft/basic.scope.scope), [dcl.fct.def.delete] (https://eel.is/c++draft/dcl.fct.def.delete), [dcl.pre] (https://eel.is/c++draft/dcl.pre), [dcl.struct.bind] (https://eel.is/c++draft/dcl.struct.bind), [stmt.iter.general] (https://eel.is/c++draft/stmt.iter.general), [cpp.embed] (https://eel.is/c++draft/cpp.embed), [depr.ellipsis.comma] (https://eel.is/c++draft/depr.ellipsis.comma)
- **cppreference links:** [C++ attribute: indeterminate](https://en.cppreference.com/w/cpp/language/attributes/indeterminate), [Undefined behavior](https://en.cppreference.com/w/cpp/language/ub), [Default-initialization](https://en.cppreference.com/w/cpp/language/default_initialization), [static_assert declaration](https://en.cppreference.com/w/cpp/language/static_assert), [Structured binding declaration](https://en.cppreference.com/w/cpp/language/structured_binding), [Function definition (deleted functions)](https://en.cppreference.com/w/cpp/language/function)
- **Prerequisites:** strict-aliasing-and-undefined-behavior, object-lifetime-and-storage, initialization-forms, reference-binding-and-temporaries, structured-bindings, preprocessor-and-translation-phases

## The one idea

Undefined behaviour gives the compiler permission to assume that something never happens. When it does happen anyway, the program can do anything, and optimizers really do delete checks and reorder code on that assumption. C++26 adds a third category between "correct" and "undefined": **erroneous behaviour**. The behaviour is still a bug, but it is *defined*. The program gets a specific, predictable result, and the implementation is encouraged to diagnose it, either while compiling or at run time.

The first and most important use is reading an uninitialized local variable. In C++23 that is undefined behaviour. In C++26 the variable holds an **erroneous value**, a fixed value chosen by the implementation, and reading it is erroneous behaviour. Your program is still wrong, but the optimizer can no longer reason from your mistake.

This is C++26. Erroneous behaviour comes from P2795R5, adopted in March 2024. The other changes covered here are P2748R5, P3144R2, P2864R2, P2865R6, P2169R4, P2573R2, P2741R3, P0963R3, P0609R3, P1061R10, P1967R14, P2809R3 and P3176R1, each named where it appears. Everything here follows N5050, the final C++26 draft sent to the international ballot. That ballot closed in October 2026 and was reported as not approved, so check the status before you quote this as published ISO C++. GCC 16 implements all of these with `-std=c++26`. The one deviation is that it only warns about mixed-enumeration arithmetic, which N5050 makes ill-formed. Clang 21 implements most of them, but it ignores `[[indeterminate]]` with a warning and treats `#embed` in C++ as an extension.

> **Mental model.** Undefined behaviour means the compiler may assume it never happens. Erroneous behaviour means it is a bug, it may be reported, and it still means one particular thing.

## Erroneous values

[basic.indet] now distinguishes two kinds of "garbage". When storage for an object with **automatic** storage duration is obtained, its bytes have **erroneous values**. Each is fixed by the implementation and does not depend on the program's state. Storage from `new`, and any variable or parameter marked `[[indeterminate]]`, starts with **indeterminate values**, as before. Static and thread-local variables are still zero-initialized.

Evaluating an erroneous value is erroneous behaviour. Evaluating an indeterminate value is still undefined behaviour. In both cases, copying an `unsigned char` or `std::byte` is allowed, and the copy inherits the erroneous or indeterminate value.

```cpp
void consume(int);

void f() {
  int a;                      // erroneous value
  int b [[indeterminate]];    // indeterminate value: opted out
  int* p = new int;           // *p has an indeterminate value

  consume(a);                 // erroneous behaviour: defined but wrong
  consume(b);                 // undefined behaviour
  consume(*p);                // undefined behaviour
  delete p;
}
```

`[[indeterminate]]` exists for performance-critical code, such as a large buffer that is about to be overwritten, where you do not want the compiler to spend time initializing it. It may appear only on the definition of a block variable with automatic storage, or on a function parameter, and it must be on the first declaration. Using it is a promise that you will write before you read. If you break the promise, you are back in undefined behaviour.

> **Standardese.** [defns.erroneous] defines erroneous behaviour as "well-defined behavior that the implementation is recommended to diagnose". Its note adds that erroneous behaviour is always the consequence of incorrect program code, and that evaluating a constant expression never exhibits behaviour specified as erroneous in the core language clauses. [intro.abstract] permits an implementation to issue a diagnostic and to terminate the program when an erroneous operation executes. Its recommended practice is to diagnose, and a note accepts diagnostics with false positives.

Constant evaluation is stricter. `constexpr int f() { int x; return x; }` cannot be used in a constant expression, because reading `x` is erroneous behaviour, and anything that exhibits erroneous behaviour is not a core constant expression.

## What GCC 16 does

The standard leaves the erroneous value unspecified. GCC 16 chooses zero. In C++26 mode, when you pass no `-ftrivial-auto-var-init=` option, GCC zero-initializes automatic variables, excluding padding bits, unless they are marked `[[indeterminate]]` or `[[gnu::uninitialized]]`. So a program that read garbage when built with `-std=c++23` reads 0 when built with `-std=c++26`. `-ftrivial-auto-var-init=pattern` chooses a recognizable non-zero pattern instead, which is usually better for finding bugs in tests.

```cpp
#include <cstdio>

[[gnu::noinline]] void scribble() { volatile int junk = 100; (void)junk; }

[[gnu::noinline]] int read_uninit() {
  int x;                // erroneous value
  return x;             // erroneous behaviour
}

int main() {
  scribble();
  std::printf("%d\n", read_uninit());  // GCC 16 -std=c++26 -O0: prints 0
}                                      // the same flags with -std=c++23 printed 100
```

> **Pitfall.** Zero is a valid value, not a diagnosis. GCC 16 does not warn at run time when an erroneous value is read, so a missing initializer can silently become "works by accident". Keep `-Wuninitialized` enabled, and run sanitizers in tests. Do not write code that relies on the zero, because another implementation, or a pattern flag, will pick something else.

## Old traps that are now compile errors

Several C++26 papers take patterns that were always bugs, often undefined behaviour, and make them ill-formed.

**Returning a reference to a temporary (P2748).** A `return` statement that binds the returned reference to a temporary is ill-formed by [stmt.return]. This catches the obvious `auto&& f() { return 42; }`, and also the subtle case where a conversion creates the temporary:

```cpp
const double& f() {
  static int x = 42;
  return x;          // error in C++26: binds the result to a temporary double
}
```

**Deleting an incomplete class (P3144).** `delete p` where `p` points to an incomplete class type used to compile, and was undefined behaviour if the class turned out to have a non-trivial destructor. It is now ill-formed.

**Mixed enumeration arithmetic (P2864).** C++20 deprecated arithmetic between two different enumeration types, and between an enumeration and a floating-point type. C++26 makes both ill-formed. Comparing two arrays with `==` or `<` was also deprecated in C++20, because it compared their addresses, and P2865R6 makes it ill-formed as well.

```cpp
enum Color { red };
enum Fruit { apple };

int a = red + apple;      // C++26: ill-formed (GCC 16 only warns)
double b = red * 1.5;     // C++26: ill-formed (GCC 16 only warns)
int c = red + 1;          // still fine: an unscoped enumeration with an integer
```

GCC 16 diagnoses the two enumeration cases only with a `-Wdeprecated-enum-enum-conversion` or `-Wdeprecated-enum-float-conversion` warning, not an error, which is a deviation from the draft. Clang 21 rejects them. Both compilers reject array comparison.

**The variadic comma (P3176).** `void f(int...)`, a C-style variadic function written without a comma before the ellipsis, is deprecated. Write `void f(int, ...)`. The motivation is `void g(auto...)`, which declares a function parameter pack, and `void h(auto......)`, which is a pack followed by C varargs.

**Trivial infinite loops (P2809).** This one goes the other way. A loop such as `while (true) ;`, whose body is empty and whose condition is a constant `true`, is no longer undefined behaviour under the forward-progress rules. [stmt.iter.general] treats it as repeated calls to `std::this_thread::yield`, so embedded code that spins forever on purpose is now correct.

## Smaller conveniences

**The placeholder `_` (P2169).** A variable, structured binding, init-capture, or non-static data member named `_` is *name-independent*. You can declare more than one in the same scope without a redefinition error. Using `_` after the second declaration is ill-formed, because the name is then ambiguous. Static variables and namespace-scope declarations do not get this treatment.

```cpp
#include <map>
#include <mutex>

std::mutex m;
std::map<int, int> table;

void g() {
  std::lock_guard _(m);                  // a guard you never refer to
  auto [_, inserted] = table.insert({1, 2});
  // auto x = _;                         // would be an error: _ is ambiguous here
  (void)inserted;
}
```

**Deleted with a reason (P2573).** `= delete("message")` puts a string literal in the diagnostic. GCC 16 prints, for example, "use of deleted function ...: use span instead".

**Computed `static_assert` messages (P2741).** The message of a `static_assert` may be any constant expression with `.size()` and `.data()` members, such as a `std::string_view`, or a `std::string` built during constant evaluation. It is evaluated only when the assertion fails. In practice you usually build such a message with `std::format`, which is not yet `constexpr` in GCC 16. GCC 16's `std::to_string` is not `constexpr` either.

**Structured bindings (P0963, P0609, P1061).** A structured binding declaration can be the condition of an `if`, `while`, or `switch`. The condition tests the whole hidden object, converted to `bool`, before the bindings are initialized. So `if (auto [ok, value] = parse(text))` works when `parse` returns a type with `explicit operator bool`, but `if (auto [it, ok] = m.insert(...))` is ill-formed, because a `std::pair` does not convert to `bool`. Each binding may also carry its own attributes, as in `auto [x [[maybe_unused]], y] = p;`. In templates, `auto [...xs] = t;` introduces a pack of bindings.

**`#embed` (P1967).** `#embed "file.bin"` expands to a comma-separated list of the file's bytes as integer literals, so `const unsigned char data[] = { #embed "data.bin" };` puts a binary file in your program without any generator script. `limit(n)`, `prefix(...)`, `suffix(...)`, and `if_empty(...)` parameters adjust the expansion, and `__has_embed` tests whether a resource exists.

**Pack indexing (P2662).** `Ts...[0]` names the first element of a pack. The pack-indexing topic covers it in full.

## Trivial relocation: adopted, then removed

P2786R13, trivial relocatability, was adopted in February 2025. It added the `trivially_relocatable_if_eligible` and `replaceable_if_eligible` contextual keywords and `std::trivially_relocate`. National-body comments then reported design problems, and in November 2025 P3920R0 removed the whole feature from C++26. N5050 contains none of it. Articles and compiler release notes written during 2025 may still describe it as part of C++26.

## What to remember

- Erroneous behaviour is well-defined but wrong. Implementations are encouraged to diagnose it and may terminate.
- An uninitialized local variable holds an erroneous value, and reading it is erroneous behaviour. Memory from `new` and `[[indeterminate]]` variables still have indeterminate values, so reading them is undefined behaviour.
- `[[indeterminate]]` is the explicit opt-out, and it brings back undefined behaviour if you read before writing.
- GCC 16 in C++26 mode zero-initializes automatic variables by default. Do not rely on it, and keep warnings and sanitizers on.
- Newly ill-formed in C++26: returning a reference bound to a temporary, `delete` of an incomplete class, enumeration-to-enumeration and enumeration-to-floating arithmetic, and comparisons between arrays.
- `_` can be declared repeatedly in one scope but not used once it is ambiguous. `= delete("why")` and computed `static_assert` messages improve diagnostics.
- A structured-binding condition tests the whole object, so it needs a type that converts to `bool`.
- Trivial relocation was added to C++26 and then removed again, so it is not in N5050.

## Concept Map Outline

- **Erroneous behaviour and C++26:** P2795's new category of defined-but-wrong behaviour, plus the C++26 rules that turn old traps into errors.
  - **Erroneous behaviour:** Well-defined behaviour that is always a bug and that implementations are recommended to diagnose.
    - **Not undefined:** The optimizer cannot assume it never happens; the program may be diagnosed or terminated.
    - **Never in constexpr:** An evaluation that would be erroneous is not a core constant expression.
  - **Uninitialized storage:** [basic.indet] now separates erroneous values from indeterminate ones.
    - **Automatic variables:** Start with erroneous values fixed by the implementation; reading one is erroneous behaviour.
    - **new and [[indeterminate]]:** Heap memory and opted-out variables start indeterminate; reading them is still UB.
    - **Static storage:** Static and thread-local variables are still zero-initialized.
    - **GCC 16 picks zero:** In C++26 mode, automatic variables are zero-filled unless -ftrivial-auto-var-init says otherwise.
  - **Newly ill-formed:** Patterns that were bugs, often UB, now rejected at compile time.
    - **Return to temporary:** P2748: a return that binds the result reference to a temporary is an error.
    - **delete incomplete type:** P3144: deleting a pointer to an incomplete class no longer compiles.
    - **Enum arithmetic:** P2864: enum with other enum or floating type is ill-formed; GCC 16 only warns.
    - **Array comparison:** P2865: comparing two arrays with == or < is ill-formed.
  - **New conveniences:** Small language additions aimed at clearer code and diagnostics.
    - **Placeholder _:** P2169: may be declared repeatedly in a scope, but a use after the second is ambiguous.
    - **delete with reason:** P2573: = delete("message") puts the message in the compiler's error.
    - **static_assert messages:** P2741: any constant with size() and data(), evaluated only on failure.
    - **Binding conditions:** P0963: if (auto [a, b] = e) tests the whole object converted to bool.
    - **#embed:** P1967: includes a binary file as a list of integer literals.
  - **Other changes:** Deprecations, loosened rules and a removal.
    - **Ellipsis comma:** P3176: void f(int...) is deprecated; write void f(int, ...).
    - **Trivial infinite loops:** P2809: while (true); is no longer UB; it behaves as repeated yield calls.
    - **Trivial relocation removed:** P2786 was adopted in 2025, then removed by P3920 in November 2025; not in N5050.

## Standardese Quiz Questions

### Question 1 [warmup] ([basic.scope.scope], [basic.lookup.general])

Under the C++26 draft, what happens with this function?

```cpp
int f() {
  int _ = 1;
  int _ = 2;
  return _;
}
```

A. It returns 2, because the second `_` hides the first
B. It returns 1, because the first `_` is the one that was named
C. It is ill-formed: `int _ = 2;` redefines `_`
D. It is ill-formed: the declarations are fine, but `return _;` is ambiguous

**Answer (D):** It is ill-formed: the declarations are fine, but `return _;` is ambiguous

**Why:** Since P2169, a local variable named `_` is name-independent, so a second `int _` in the same scope is not a redefinition. Once there are two such declarations, though, a use of `_` finds both, and the program is ill-formed. Declaring `_` several times is meant for objects you never refer to, such as lock guards or ignored structured bindings. A single `_` can still be used normally.

### Question 2 [warmup] ([basic.indet], [defns.erroneous], [intro.abstract])

Under the C++26 draft, what is the status of the initialization of `y`?

```cpp
void g() {
  int x;
  int y = x;
  (void)y;
}
```

A. Undefined behavior, as in C++23
B. Erroneous behavior: `x` holds an implementation-chosen erroneous value
C. Well-defined: `y` is guaranteed to be 0
D. Ill-formed: an uninitialized variable cannot be read

**Answer (B):** Erroneous behavior: `x` holds an implementation-chosen erroneous value

**Why:** In C++26 the bytes of an automatic variable without an initializer have erroneous values, and evaluating an erroneous value is erroneous behavior. That behavior is well-defined, but it is a bug that the implementation is recommended to diagnose and may terminate on. The value is fixed by the implementation, not guaranteed to be 0, although GCC 16 happens to choose 0. It is not ill-formed, because the program may never execute `g`.

### Question 3 [warmup] ([depr.ellipsis.comma], [dcl.fct])

Under the C++26 draft, what does this declaration declare?

```cpp
void f(int...);
```

A. A function template with a parameter pack of `int`s
B. Nothing: it is ill-formed in C++26
C. A C-style variadic function taking an `int` followed by `...`; the spelling without a comma is deprecated
D. A function taking a single array of `int` of unknown bound

**Answer (C):** A C-style variadic function taking an `int` followed by `...`; the spelling without a comma is deprecated

**Why:** Without a comma, `int...` is still the C-style ellipsis after one `int` parameter, exactly like `void f(int, ...)`. P3176 deprecated that spelling, so it is listed in [depr.ellipsis.comma] but remains valid. The reason is that `void g(auto...)` declares a function parameter pack, and the two forms are easy to confuse. GCC 16 and Clang 21 both warn about it in C++26 mode.

### Question 4 [tricky] ([dcl.struct.bind], [stmt.pre])

Under the C++26 draft, what happens with this code?

```cpp
#include <map>

bool add(std::map<int, int>& m) {
  if (auto [it, ok] = m.insert({1, 2}))
    return ok;
  return false;
}
```

A. It is ill-formed: the condition converts the whole `std::pair` to `bool`, and `pair` has no such conversion
B. It compiles, and the condition tests `ok`, the last binding
C. It compiles, and the condition tests `it`, the first binding
D. It is ill-formed: structured bindings cannot appear in a condition

**Answer (A):** It is ill-formed: the condition converts the whole `std::pair` to `bool`, and `pair` has no such conversion

**Why:** P0963 allows a structured binding declaration as a condition. The value tested is the hidden object that the bindings refer to, contextually converted to `bool` before the bindings are initialized. `std::pair<iterator, bool>` is not convertible to `bool`, so this is ill-formed. It works for types designed for it, such as a result type with an `explicit operator bool`. Neither binding is ever tested on its own.

### Question 5 [tricky] ([basic.indet], [dcl.attr.indet], [basic.start.static])

Under the C++26 draft, which marked read has undefined behavior rather than erroneous behavior?

```cpp
void use(int);

void h() {
  int a;
  use(a);                    // (1)
  int b [[indeterminate]];
  use(b);                    // (2)
  static int c;
  use(c);                    // (3)
}
```

A. (1)
B. (2)
C. (3)
D. None: in C++26 none of these reads has undefined behavior

**Answer (B):** (2)

**Why:** `[[indeterminate]]` opts the variable out of erroneous values, so `b` starts with an indeterminate value, and evaluating it is undefined behavior. `a` has an erroneous value, so read (1) is erroneous behavior, which is defined but a bug. `c` has static storage duration and is zero-initialized, so read (3) is correct and reads 0. Memory from `new` also still starts out indeterminate.

### Question 6 [tricky] ([stmt.return], [class.temporary], [dcl.init.ref])

Under the C++26 draft, what happens with this function?

```cpp
const double& value() {
  static int x = 42;
  return x;
}
```

A. It returns a reference to `x`, viewed as a `double`
B. It compiles, but using the result is undefined behavior
C. Its result refers to a temporary whose lifetime is extended to match the reference
D. It is ill-formed: the returned reference would be bound to a temporary

**Answer (D):** It is ill-formed: the returned reference would be bound to a temporary

**Why:** Binding a `const double&` to an `int` lvalue creates a temporary `double` holding 42.0. That temporary would die at the end of the `return`, leaving a dangling reference. Until C++23 this compiled and using the result was undefined behavior. P2748 makes a `return` statement that binds the returned reference to a temporary ill-formed, and lifetime extension never applies to a returned reference.

### Question 7 [standardese] ([expr.delete], [diff.cpp23])

Under the C++26 draft, what is the status of this translation unit?

```cpp
struct Widget;

void destroy(Widget* p) {
  delete p;
}
```

A. Well-formed, and the behavior is undefined only if `Widget` turns out to have a non-trivial destructor
B. Ill-formed
C. Well-formed, and `delete` calls `operator delete` without running a destructor
D. Ill-formed, no diagnostic required

**Answer (B):** Ill-formed

**Why:** Up to C++23, deleting a pointer to an incomplete class type was allowed, and it had undefined behavior when the complete class had a non-trivial destructor or a class-specific `operator delete`. P3144 changes [expr.delete] so that the object being deleted must not have incomplete class type, which makes the program ill-formed with a required diagnostic. GCC 16 and Clang 21 both reject it in C++26 mode.

### Question 8 [standardese] ([expr.const], [defns.erroneous], [basic.indet])

Under the C++26 draft, what is the status of the declaration of `v`?

```cpp
constexpr int f() {
  int x;
  return x;
}

constexpr int v = f();
```

A. Well-formed: `v` is initialized with the implementation's erroneous value
B. Well-formed: `v` is 0
C. Ill-formed: evaluating `f()` would read an erroneous value, so it is not a constant expression
D. Undefined behavior during compilation

**Answer (C):** Ill-formed: evaluating `f()` would read an erroneous value, so it is not a constant expression

**Why:** An expression whose evaluation would have undefined or erroneous behavior, as specified in the core language clauses, is not a core constant expression. Reading `x` evaluates an erroneous value, so `f()` cannot initialize a `constexpr` variable, and the declaration is ill-formed. The definition of erroneous behavior states this directly: a constant expression never exhibits behavior specified as erroneous. The function itself is fine, because `int x;` without an initializer is allowed in a `constexpr` function.
