1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. Why does declaring a move constructor noexcept change how…

Added 30 Nov -0001

Why does declaring a move constructor noexcept change how std::vector relocates its elements when the buffer grows?

  1. vector then moves elements instead of copying them, which keeps the strong guarantee Correct answer
  2. vector refuses to grow the buffer at all once the move constructor is declared noexcept
  3. vector starts using memcpy on the elements, since noexcept proves they are trivial
  4. vector falls back to copying, because noexcept disables the move constructor entirely

Correct answer

A. vector then moves elements instead of copying them, which keeps the strong guarantee

15 views

MCQSEXAM

Explanation

Move_if_noexcept is the rule at work: vector must preserve its strong exception guarantee, so it relocates by move only when the move constructor is noexcept or copying is unavailable. Otherwise every reallocation copies the elements, so a missing noexcept on a hand-written move constructor quietly costs performance. Declaring a destructor or a move member by hand can also suppress the implicitly declared move constructor.

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