|
|
|
|
|
by dragontamer
2053 days ago
|
|
unique_ptr<Blah> foo = make_unique(...);
unique_ptr<Blah> bar = std::move(foo);
// foo is now effectively destroyed as part of the move;
foo->something(); // Will error, probably seg-fault. Foo is now "empty"
The value foo used to point at is preserved. But foo itself was destroyed from the std::move(foo); The foo->something() is almost certainly a null-pointer exception or seg-fault in any sane implementation of C++11.--------- Example 2: int main(){
vector<int> foo = {1, 2, 3, 4, 5};
vector<int> bar = std::move(foo);
cout << foo.size() << endl;
cout << bar.size() << endl;
return 0;
}
Notice: foo.size() is 0. The foo-vector was DESTROYED by the std::move(foo);. |
|
In fact there are people who have wanted destructive moves in C++, because they found the current non-destructive moves inadequate, but I don't believe the feature has ever been added. Look up destructive moves to find discussions on the issue.