CWG Issue 1290 (original) (raw)

An object of type std::initializer_list is constructed from an initializer list as if the implementation allocated an a temporary array of _N_elements of type E, where...

The lifetime of the array is the same as that of theinitializer_list object. The array has the same lifetime as any other temporary object (6.7.7 [class.temporary]), except that initializing an initializer_list object from the array extends the lifetime of the array exactly like binding a reference to a temporary. [Example:

typedef std::complex cmplx; std::vector v1 = { 1, 2, 3 };

void f() { std::vector v2{ 1, 2, 3 }; std::initializer_list i3 = { 1, 2, 3 }; }

struct A { std::initializer_list i4; A(): i4{1,2,3} { } // creates an A with a dangling reference };

For v1 and v2, the initializer_list objectis a parameter in a function call, so the andarray created for { 1, 2, 3 } have hasfull-expression lifetime. For i3, theinitializer_list object is a variable, so the and array have automatic persists for the lifetime of the variable. For i4, theinitializer_list object is initialized in a constructor's_ctor-initializer_, so the array persists only until the constructor exits, and so any use of the elements of i4 after the constructor exits produces undefined behavior. —_end example_] [Note: The implementation is free to allocate the array in read-only memory if an explicit array with the same initializer could be so allocated. —_end note_]