https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91548
Bug ID: 91548
Summary: Regression in constexpr evaluation of std::array
Product: gcc
Version: 9.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: barry.revzin at gmail dot com
Target Milestone: ---
The following compiles on gcc 9.2 -std=c+=2a but fails on gcc trunk. This
example is based on the implementation of std::array:
using size_t = decltype(sizeof(0));
template <typename T, size_t N>
constexpr T& impl(T const (&array)[N], size_t index) {
return const_cast<T&>(array[index]);
}
template <typename T, size_t N>
struct my_array {
constexpr T& operator[](size_t i) { return impl(elems, i); }
constexpr T const& operator[](size_t i) const { return elems[i]; }
T elems[N];
};
bool f(int i) {
static constexpr auto table = []() {
my_array<bool, 256> arr = {};
arr[2] = true;
return arr;
}();
return table[i];
}
The error (https://godbolt.org/z/WKpVJa) is:
<source>: In function 'bool f(int)':
<source>:20:7: in 'constexpr' expansion of '<lambda closure
object>f(int)::<lambda()>{}.f(int)::<lambda()>()'
<source>:18:16: error: modifying a const object 'arr.my_array<bool,
256>::operator[](2)' is not allowed in a constant expression
18 | arr[2] = true;
<source>:20:7: note: originally declared 'const' here
20 | }();
| ^
Compiler returned: 1