https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92505
Bug ID: 92505
Summary: Using mutable in constexpr
Product: gcc
Version: 10.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: Casey at Carter dot net
Target Milestone: ---
GCC trunk does not accept this well-formed program in -std=c++2a mode:
constexpr int f() {
struct {
mutable int i = 41;
} s;
auto const& cs = s;
return ++cs.i;
}
int main() {
constexpr int i = f();
return 42 - i;
}
Diagnosing (https://godbolt.org/z/n259tb):
<source>: In function 'int main()':
<source>:10:24: in 'constexpr' expansion of 'f()'
<source>:10:25: error: mutable 'f()::<unnamed struct>::i' is not usable in
a constant expression
10 | constexpr int i = f();
| ^
Compiler returned: 1
AFAICS this is well-formed back to C++14 since it applies lvalue-to-rvalue
conversion to "a non-volatile glvalue of literal type that refers to a
non-volatile object whose lifetime began within the evaluation of e;".