1

I have the following code:

void g(int& i) {
    std::cout << "g(int&)" << std::endl;
}
void g(int&& i) {
    std::cout << "g(int&&)" << std::endl;
}
void f(int&& i) {
    g(i);
}
int main() {
    f(3);  // g(int&)
}

As you can see, the parameter i in the f function is an rvalue reference, so when it calls g(i), the corresponding g function which takes an rvalue reference should be called(i.e. the result should be g(int&&)), but this is not the case. Actually, g(int& i) was called. Why?

My guess is that when we are inside the f function, i becomes an lvalue instead of an rvalue, is that correct? Are lvalue and rvalue interchangeable?

Searene
  • 25,920
  • 39
  • 129
  • 186
  • 4
    Your assumption is correct. You have the `std::move` every step of the way towards the final destination. – DeiDei Sep 03 '18 at 01:01
  • 3
    While i in f function binds to rvalue, it is a lvalue (it has a name), so, when you use it to call another function, it will binds to lvalue unless you ask it to become a rvalue (`std::move`) – Amadeus Sep 03 '18 at 01:07

0 Answers0