2

https://stackoverflow.com/a/18300974/462608

If you pass an rvalue reference to another function, you are passing a named object, so, the object isn't received like a temporal object.

void some_function(A&& a)
{
   other_function(a);
}

The object a would be copied to the actual parameter of other_function. If you want the object a continues being treated as a temporary object, you should use the std::move function:

other_function(std::move(a));

What would be the type of argument of other_function?

It can't be other_function( A&& xyz ) because if that had been the case then std::move wouldn't be required?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • 2
    If it's `other_function( A&& xyz )`, then `std::move` is required to convert `a` to rvalue; which could be passed to `other_function` taking rvalue-reference. – songyuanyao Sep 01 '20 at 12:37

2 Answers2

3

You can test it with this simple code

#include <iostream>

struct A{};

void other_function(A&) {
    std::cout << "L-value\n";
}

void other_function(A&&) {
    std::cout << "R-value\n";
}

void some_function(A&& a)
{
   other_function(a); // a is an L-value
}

int main() {
    A a;
    some_function(std::move(a));
    other_function(std::move(a));
    return 0;

}

Output:

L-value
R-value

This is a test for perfect forwarding:

#include <iostream>

struct A{};

void other_function(A&) {
    std::cout << "L-value\n";
}

void other_function(A&&) {
    std::cout << "R-value\n";
}

template<typename T>
void some_function(T&& a)
{
   other_function(std::forward<T>(a));
}

int main() {
    A a;
    some_function(a);
    some_function(std::move(a));
    return 0;

}

Output

L-value
R-value
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
2

An rvalue reference is not an rvalue. It's confusing because an rvalue was passed in the argument list of some_function; but a, as with other named values, is an lvalue. std::move is required to take a (an rvalue reference, which is an lvalue) and convert it into an rvalue.

It's probably easiest if you take function calls out of the example.

int main() {
    int&& a = 3; // compiles: binding to an rvalue
    int&& b = a; // doesn't compile: a is not an rvalue
}

You can't initialize an rvalue reference from a named rvalue reference, because it doesn't have the right value category.

Sneftel
  • 40,271
  • 12
  • 71
  • 104