...
The C++ Standard, [basic.life], paragraph 5 [ISO/IEC 14882-2014], describes the lifetime rules for pointers:
...
A std::initializer_list<>
object is constructed from an initializer list as though the implementation allocated a temporary array and passed it to the std::initializer_list<>
constructor. This temporary array has the same lifetime as other temporary objects except that initializing a std::initializer_list<>
object from the array extends the lifetime of the array exactly like binding a reference to a temporary [ISO/IEC 14882-2014]. In this noncompliant code example, a member variable of type std::initializer_list<int>
is list-initialized within the constructor's ctor-initializer. Under these circumstances, the conceptual temporary array's lifetime ends once the constructor exits, and so accessing any elements of the std::initializer_list<int>
member variable results in undefined behavior.
...
Bibliography
[Coverity 2007] | |
[ISO/IEC 14882-2014] | Subclause 3.8, "Object Lifetime" Subclause 8.5.4, "List-Initialization" |
...