https://wg21.link/cwg2481
constexpr const int &r = 42; static_assert(r == 42);
Or rather, int main() { constexpr const int &r = 42; static_assert(r == 42); // { dg-bogus "" } } [expr.const]/4.7 says that "a temporary object of non-volatile const-qualified literal type whose lifetime is extended to that of a variable that is usable in constant expressions" is usable in a constant expression.
(In reply to Jason Merrill from comment #2) > Or rather, > > int main() > { > constexpr const int &r = 42; > static_assert(r == 42); // { dg-bogus "" } > } > > [expr.const]/4.7 says that "a temporary object of non-volatile > const-qualified literal type whose lifetime is extended to that > of a variable that is usable in constant expressions" is usable in a > constant expression. This was actually CWG 2126 which we don't seem to fully implement: struct A { int n; }; const A &a = {1}; // const temporary A &b = (A &)(const A &)A{1}; // const temporary A &&c = (A &&)(const A &)A{1}; // const temporary static_assert(a.n == 1, ""); static_assert(b.n == 1, ""); static_assert(c.n == 1, "");
Turned out we already implement CWG 2481. If anyone has a test for something we don't handle, please open a PR.