1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. Since C++17, what is guaranteed when a function returns a…

Added 30 Nov -0001

Since C++17, what is guaranteed when a function returns a prvalue such as return Widget{};?

  1. The object is built directly in the caller's storage, with no move at all Correct answer
  2. One move construction happens, but the copy constructor is elided
  3. The move constructor must still be accessible even though it is never called
  4. The compiler may elide the move, but this remains optional for it

Correct answer

A. The object is built directly in the caller's storage, with no move at all

13 views

MCQSEXAM

Explanation

C++17 made copy elision mandatory for prvalue initialisation: the temporary is materialised directly in the destination, so no copy or move constructor is invoked. The type therefore need not even have an accessible move constructor for such a return. Named return value optimisation (NRVO) remains an optional optimisation, which is where the older must-be-movable intuition still applies.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? 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