Practice mode C++

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.