This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 118e. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.
2025-11-05
[Moved to DR at the October, 2012 meeting.]
A question has arisen over expected behavior when aninitializer_list is a non-static data member of a class.Initialization of aninitializer_list is defined in terms ofconstruction from an implicitly allocated array whose lifetime "is thesame as that of theinitializer_list object". That wouldmean that the array needs to live as long as theinitializer_list does, which would on the face of it appearto require the array to be stored in something like astd::unique_ptr<T[]> within the same class (if themember is initialized in this manner).
It would be surprising if that was the intent, but it would makeinitializer_list usable in this context.
It would also be reasonable if this behaved similarly to bindingtemporaries to reference members (i.e., "temporary bound to areference member in a constructor'sctor-initializer(11.9.3 [class.base.init]) persists until the constructor exits."),though this approach would probably prevent use of aninitializer_list member in that context.
Proposed resolution (February, 2012):
Change 9.5.5 [dcl.init.list] paragraphs 5-6 asfollows:
An object of typestd::initializer_list<E> isconstructed from an initializer list as if the implementationallocated
ana temporary array ofNelements of typeE, where...
The lifetime of the array is the same as that of theinitializer_list object.The array has the samelifetime as any other temporary object (6.8.7 [class.temporary]),except that initializing aninitializer_list object from thearray extends the lifetime of the array exactly like binding areference to a temporary. [Example:typedef std::complex<double> cmplx; std::vector<cmplx> v1 = { 1, 2, 3 }; void f() { std::vector<cmplx> v2{ 1, 2, 3 }; std::initializer_list<int> i3 = { 1, 2, 3 }; } struct A { std::initializer_list<int> i4; A(): i4{1,2,3} { } // creates anA with a dangling reference };Forv1 andv2, theinitializer_list objectis a parameter in a function call, so the
andarray created for{ 1, 2, 3 }havehasfull-expression lifetime. Fori3, theinitializer_list objectis a variable, so theandarrayhave automaticpersists forthe lifetimeof the variable. Fori4, theinitializer_list object is initialized in a constructor'sctor-initializer, so the array persists only until theconstructor exits, and so any use of the elements ofi4 afterthe constructor exits produces undefined behavior. —endexample] [Note: The implementation is free to allocate thearray in read-only memory if an explicit array with the sameinitializer could be so allocated. —end note]
Change 6.8.7 [class.temporary] paragraph 5 as follows:
The second context is when a reference is bound to a temporary.[Footnote: The same rules apply to initialization of aninitializer_list object (9.5.5 [dcl.init.list]) with itsunderlying temporary array. —end footnote] Thetemporary to which...