Title 1599. Lifetime of initializer_list underlying array
Status open Section 11.6.4 [dcl.init.list]
Submitter Johannes Schaub

Created on 2012-02-10.00:00:00 by admin, last changed by admin.

msg4822 (view) 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.

msg4240 (view) Date: 2018-02-27.00:00:00

The normative wording of 11.6.4 [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 (11.6 [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.)

Date User Action Args
2018-02-27 00:00:00adminsetsection: 8.6.4 [dcl.init.list] -> 11.6.4 [dcl.init.list]
2017-02-06 00:00:00adminsetsection: 8.5.4 [dcl.init.list] -> 8.6.4 [dcl.init.list]
2014-03-03 00:00:00adminsetmessages: + msg4822
2012-02-10 00:00:00admincreate