1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. What is the lifetime of the backing array of a…

Added 30 Nov -0001

What is the lifetime of the backing array of a std::initializer_list bound to a local variable?

  1. It has static storage duration, so returning the list by value is safe
  2. It ends with the full-expression that created it, like any temporary array Correct answer
  3. It ends only with the enclosing block, exactly as a local const array does
  4. It lasts as long as the initializer_list object that was copied from it

Correct answer

B. It ends with the full-expression that created it, like any temporary array

21 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs