1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. A const std::string s is passed to a constructor call as…

Added 30 Nov -0001

A const std::string s is passed to a constructor call as std::move(s). Which constructor runs, and why?

  1. The move constructor, because std::move casts its argument to an rvalue reference
  2. A compile-time error, because std::move is deleted for const-qualified arguments by design
  3. The move constructor, because const objects are always treated as expiring values here
  4. The copy constructor, because std::move yields a const rvalue that cannot bind to T&& Correct answer

Correct answer

D. The copy constructor, because std::move yields a const rvalue that cannot bind to T&&

14 views

MCQSEXAM

Explanation

std::move is only a cast to an rvalue reference; the overload set still decides what happens, and a const T&& cannot bind to T&&, so the copy constructor is selected. Code that looks efficient then pays for a full copy. The same rule prevents moving from a const data member inside a const member function unless the member is mutable.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

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