Assuming standard headers and using namespace std, what does this C++ code print? void f(int&) { cout << “L”; } void f(int&&) { cout << “R”; } int main() { int x = 1; f(x); f(2); f(std::move(x)); }
Correct answer B. LRR
Explanation
x is an lvalue, so it binds to the int& overload. The literal 2 is a prvalue and binds to int&&. std::move(x) does not move anything; it casts x to an xvalue (an rvalue), so it also selects the int&& overload. This lvalue/rvalue overloading is exactly how move constructors and move assignment are chosen.
Answer checked How we write and check questions
Discussion