This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 116a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-12-19


1599. Lifetime of initializer_list underlying array

Section: 9.4.5  [dcl.init.list]     Status: CD4     Submitter: Johannes Schaub     Date: 2012-02-10

[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.)

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.