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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…