https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94074
Bug ID: 94074 Summary: bogus modifying a const object error with const COMPONENT_REF Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: mpolacek at gcc dot gnu.org Target Milestone: --- typedef decltype (sizeof (0)) size_t; template <typename E, size_t N> struct array { constexpr const E &operator[](size_t n) const noexcept { return elems[n]; } E elems[N]; }; template <typename T> struct S { using U = array<T, 4>; U m; constexpr S(int) : m{} { const_cast<int &>(const_cast<const U &>(m)[0]) = 42; } }; constexpr S<int> p = { 10 }; $ ./cc1plus -quiet q.C q.C:20:27: in ‘constexpr’ expansion of ‘S<int>(10)’ q.C:16:52: error: modifying a const object ‘(int&)(&(&(const U&)(&((S<int>*)this)->S<int>::m))->array<int, 4>::operator[](0))’ is not allowed in a constant expression 16 | const_cast<int &>(const_cast<const U &>(m)[0]) = 42; | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~ q.C:20:27: note: originally declared ‘const’ here 20 | constexpr S<int> p = { 10 }; | ^ ISTM we shouldn't error here.