Title
Lifetime extension for function-style cast to reference type
Status
open
Section
6.7.7 [class.temporary]
Submitter
Hubert Tong

Created on 2024-10-18.00:00:00 last changed 2 months ago

Messages

Date: 2024-10-18.20:05:06

Suggested resolution:

Change in 6.7.7 [class.temporary] paragraph 6 as follows:

  • a
  • an explicit type conversion (functional notation) (7.6.1.4 [expr.type.conv]) to a reference type whose initializer is a braced-init-list where the reference is
    • bound directly to the glvalue result of one of these expressions that is the sole element of the braced-init-list or
    • bound to the result of a temporary materialization conversion,
  • a conditional expression (7.6.16 [expr.cond]) that is a glvalue where the second or third operand is one of these expressions, or
Date: 2024-10-18.20:05:06

(From submission #625.)

Issue 2894 will clarify that a function-style cast to reference type produces a glvalue, not a prvalue. However, 6.7.7 [class.temporary] does not specify lifetime-extension for this case, even though implementations uniformly do extend the lifetime.

For example:

  int glob;
  struct A {
   constexpr ~A() { p = &glob; }
   int *p;
  };
  constexpr int f() {
   typedef const A &AR;
   const A &ref = AR{0};
   delete ref.p;
   return 0;
  }
  extern constexpr int x = f(); // okay
History
Date User Action Args
2024-10-18 10:47:52adminsetmessages: + msg7847
2024-10-18 00:00:00admincreate