#include <vector>
using namespace std;
struct TempData {
vector<int> data;
TempData() {
for(int i = 0; i < 100; i++) data.push_back(i);
}
// vector<int> GetData() { // function 1
// return move(data);
// }
vector<int>&& GetData() { // function 2
return move(data);
}
};
int main() {
vector<int> v;
{
TempData td;
v = td.GetData();
}
}
What is the difference between function 1
and function 2
?
Will function 1
construct a temp vector
with move(data)
and then assign the temp vector
to v
?
No more details to add...
In your little test case there is likely no difference. The extra temporary object will almost certainly be elided. And the v
in main will hold the contents of the member variable.
But in the general case:
Version 1 will definitely leave the member data
in some unspecified "empty" state. Even when the function return value is discarded.
Version 2 may leave the member in some unspecified empty state, or it may not. For instance, if the function is called and its return value discarded, the member will be unchanged. This is why it can be said that std::move
by itself doesn't move anything.