https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85125
Bug ID: 85125 Summary: constant expression with const_cast UB does not emit error Product: gcc Version: 5.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: john at mcfarlane dot name Target Milestone: --- Tested in godbolt.org ([example](https://godbolt.org/g/dhtXmH)) with x86-64 versions from 5.1 to 7.3 and trunk. Options are `-Wall -Wextra` but can be reproduced without. The following code compiles: constexpr int f() { int const i = 77; const_cast<int&>(i) = 88; return i; } static_assert(f()==77, ""); >From reading of http://eel.is/c++draft/dcl.type.cv#4 the `const_cast` line above results in UB and from http://eel.is/c++draft/expr.const#2.6 an expression causing UB cannot be a constant expression. Expected: an error is emitted on the `const_cast` line explaining that a constant value is being written to. Actual: code compiles without warnings or errors and the `const_cast` line appears to be ignored. (When `constexpr` is removed, produces code for a function that returns 77.)