Where, specifically, does the standard state that modifying a const object is undefined behaviour?

Viewed 73

The following example is well known to exhibit undefined behaviour:

T const x = T();
T& r = any_suitable_conversion_to_Tref(x); // fine
r = T(); // UB

This is confirmed for const_cast by cppreference:

const_cast makes it possible to form a reference or pointer to non-const type that is actually referring to a const object or a reference or pointer to non-volatile type that is actually referring to a volatile object. Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue results in undefined behavior.

However, it says nothing about e.g. c-style conversions and it doesn't quote the standard (cppreference generally never does).

Where exactly does the C++ standard forbid this?

1 Answers

The C++17 standard states in chapter 10 "Declarations" under [dcl.type.cv] 10.1.7:

  1. Except that any class member declared mutable (10.1.1) can be modified, any attempt to modify a const object during its lifetime (6.8) results in undefined behavior.
Related