Constant initialization via non-constant temporary
7.7 [expr.const]
Daniel Kr├╝gler

Created on 2012-03-17.00:00:00 last changed 90 months ago


Date: 2013-04-15.00:00:00

Proposed resolution (April, 2013):

This issue was rendered moot by paper N3652, adopted at the April, 2013 meeting.

Date: 2013-02-15.00:00:00

Additional note (February, 2013):

The intent is that non-const references to temporaries should be allowed within constant expressions, e.g., across constexpr function calls, but not as the result of a constant expression.

Date: 2012-03-17.00:00:00

The following initializations appear to be well-formed:

  struct C {
   int m;
   constexpr C(int m ) : m{m} {};
   constexpr int get() { return m; };

  C&& rr = C{1};
  constexpr int k1 = rr.get();

  const C& cl = C{1};
  constexpr int k2 = cl.get();

They appear to fall under the bullet of 7.7 [expr.const] paragraph 2,

  • an lvalue-to-rvalue conversion (7.3.2 [conv.lval]) unless it is applied to

    • ...

    • a non-volatile glvalue of literal type that refers to a non-volatile temporary object whose lifetime has not ended, initialized with a constant expression;

The problem in this example is that the referenced temporary object is not a constant, so it would be well-defined for intervening code to modify its value before it was used in the later initialization.

Date User Action Args
2014-03-03 00:00:00adminsetstatus: wp -> cd3
2014-01-20 00:00:00adminsetmessages: + msg4791
2014-01-20 00:00:00adminsetstatus: drafting -> wp
2013-03-18 00:00:00adminsetmessages: + msg4256
2012-11-03 00:00:00adminsetstatus: open -> drafting
2012-03-17 00:00:00admincreate