Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "return-by-rvalue-ref" & "return-by-value" when you return using std::move?

Considering the following code:

#include <iostream>
using namespace std;

struct I {
    I(I&& rv) { cout << "I::mvcotr" << endl; }
};

struct C {
    I i;
    I&& foo() { return move(i) };
    }
};

int main() {
    C c;
    I i = c.foo();
}

C contains I. And C::foo() allows you to move I out of C. What is the difference between the member function used above:

I&& foo() { return move(i) }; // return rvalue ref

and the following replacement member function:

I foo() { return move(i) }; // return by value

To me, they seem to do the same thing: I i = c.foo(); leads to a call to I::I(I&&);.

What consequences will there be that is not covered in this example?

like image 310
Benji Mizrahi Avatar asked May 24 '13 09:05

Benji Mizrahi


1 Answers

Considerations aside on whether the program you wrote actually makes sense (moving from a data member is awkward - but OK, perhaps there are some use cases), in this case the two versions of that function end up doing the same thing.

As general practice, however, you should prefer returning by value, because in many cases it allows the compiler to perform copy elision and elide the calls to the move constructor of the returned type, as permitted by paragraph 12.8/31 of the C++11 Standard.

Copy elision allows the compiler to create the return value of the function directly in the object which should be initialized from the function's return value.

Therefore, as a general guideline, prefer returning by value.

like image 91
Andy Prowl Avatar answered Nov 15 '22 08:11

Andy Prowl