A const std::string s is passed to a constructor call as std::move(s). Which constructor runs, and why?
Correct answer
D. The copy constructor, because std::move yields a const rvalue that cannot bind to T&&
14 views
D. The copy constructor, because std::move yields a const rvalue that cannot bind to T&&
14 views
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.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.