Title
Lifetime of initializer_list underlying array
Status
cd4
Section
9.4.5 [dcl.init.list]
Submitter
Johannes Schaub

Created on 2012-02-10.00:00:00 last changed 40 months ago

Messages

Date: 2014-02-15.00:00:00

Notes from the February, 2014 meeting:

The resolution of issue 1299, clarifying the relationship between temporary expressions and temporary objects, is relevant to this issue.

Date: 2016-06-15.00:00:00

[Adopted as paper P0135R1 at the June, 2016 meeting.]

The normative wording of 9.4.5 [dcl.init.list] regarding the lifetime of the array underlying an initializer_list object does not match the intent as specified in the example in paragraph 6 of that section, even after application of the resolution of issue 1290. That example contains the lines:

  void f() {
    std::initializer_list<int> i3 = { 1, 2, 3 };
  }

The commentary indicates that the lifetime of the array created for the initialization of i3 “persists for the lifetime of the variable.” However, that is not the effect of the normative wording. According to paragraph 3,

if T is a specialization of std::initializer_list<E>, an initializer_list object is constructed as described below and used to initialize the object according to the rules for initialization of an object from a class of the same type (9.4 [dcl.init]).

In other words, the underlying array for {1,2,3} in the example is associated with the temporary and shares its lifetime; its lifetime is not extended to that of the variable.

(See also issue 1565.)

History
Date User Action Args
2020-12-15 00:00:00adminsetstatus: open -> cd4
2014-03-03 00:00:00adminsetmessages: + msg4909
2012-02-10 00:00:00admincreate