What is the lifetime of the backing array of a std::initializer_list bound to a local variable?
Correct answer
B. It ends with the full-expression that created it, like any temporary array
21 views
B. It ends with the full-expression that created it, like any temporary array
21 views
The backing array of a std::initializer_list is a temporary whose lifetime ends at the end of the full-expression that created it, so copying the list object does not extend the array's life. Returning a braced list from a function therefore dangles unless the elements are copied into a container such as std::vector. The elements are also const objects, so the list can never be used to modify them.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.