Title
Lifetime extension for temporaries in expansion statements
Status
ready
Section
6.8.7 [class.temporary]
Submitter
Jakub Jelinek

Created on 2025-07-07.00:00:00 last changed 1 month ago

Messages

Date: 2025-11-06.20:15:22

Proposed resolution (approved by CWG 2025-11-04):

Change in 6.8.7 [class.temporary] paragraph 7 and paragraph 8 as follows:

The fourth context is when a temporary object is created in the for-range-initializer of either a range-based for statement or an enumerating expansion statement (8.7 [stmt.expand]). If such a temporary object would otherwise be destroyed at the end of the for-range-initializer full-expression, the object persists for the lifetime of the reference initialized by the for-range-initializer.

The fifth context is when a temporary object is created in an element E of the expansion-init-list of an enumerating expansion statement (8.7 [stmt.expand]). If such a temporary object would otherwise be destroyed at the end of the full-expression of E, the object persists for the lifetime of the for-range-declaration initialized from E.

The fifth sixth context is when a temporary object is created in the expansion-initializer of an iterating or a destructuring expansion statement. If such a temporary object would otherwise be destroyed at the end of that expansion-initializer, the object persists for the lifetime of the reference initialized by the expansion-initializer, if any.

The sixth seventh context is when ...

Date: 2025-11-05.12:07:23
N5028 comment USĀ 21-038

(From submission #724.)

Subclause 6.8.7 [class.temporary] paragraph 7 and paragraph 8 specify:

The fourth context is when a temporary object is created in the for-range-initializer of either a range-based for statement or an enumerating expansion statement (8.7 [stmt.expand]). If such a temporary object would otherwise be destroyed at the end of the for-range-initializer full-expression, the object persists for the lifetime of the reference initialized by the for-range-initializer.

The fifth context is when a temporary object is created in the expansion-initializer of an iterating or destructuring expansion statement. If such a temporary object would otherwise be destroyed at the end of that expansion-initializer , the object persists for the lifetime of the reference initialized by the expansion-initializer, if any.

There are a number of problems with this phrasing:

  • The original idea of lifetime extension in a for-range-initializer was to ensure that temporaries defining a range would survive across all iterations. For an enumerating expansion statement, there is no overarching range expression; each element of the expression-list is considered separately. Furthermore, there is no for-range-initializer in the corresponding grammar to start with.
  • For an iterating expansion statement, the involved variables are constexpr, so no dangling pointers or similar could escape from the constant evaluation to start with. The necessity for temporary lifetime extension is thus unclear.
History
Date User Action Args
2025-11-06 23:04:52adminsetstatus: tentatively ready -> ready
2025-11-05 12:07:23adminsetmessages: + msg8211
2025-11-05 12:07:23adminsetstatus: open -> tentatively ready
2025-07-07 00:00:00admincreate