Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 lvalue, rvalue and std::move()

I have the following code:

#include <iostream>
using namespace std;
void test(int& a) {
    cout << "lvalue." << endl;
}
void test(int&& a) {
    cout << "rvalue" << endl;
}
int main(int argc, char *argv[]) {
    int a = 1;
    int&& b = 2;
    test(a);
    test(1);
    test(std::move(a));
    test(b);
}

which outputs:

lvalue.
rvalue
lvalue.
lvalue.

std::move() and int&& are rvalue references, I wonder why test(std::move(a)) and test(b) output lvalue? Is it related with signature matching and function overloading?

like image 810
Simon X. M. LIU Avatar asked Jul 23 '26 19:07

Simon X. M. LIU


1 Answers

The output should be:

lvalue.
rvalue
rvalue
lvalue.

There is a very important distinction to be made between expressions which are rvalues and expressions whose type is an rvalue reference. The type of b is an rvalue reference to int, but the expression b is an lvalue; it is a variable, you can take its address. This is why the final line of output is lvalue rather than rvalue. In order to change it to an rvalue, you should call std::move on it:

test(std::move(b));
like image 69
TartanLlama Avatar answered Jul 25 '26 10:07

TartanLlama



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!