4 ms·
Going off-topic, but note: C++11 moves are non-destructive.
by mehrdadn 6y ago
Going off-topic, but note: C++11 moves are non-destructive.
- dragontamer 6y agounique_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);.
- mehrdadn 6y agounique_ptr's destructor happens to do nothing in a moved-from state. The fact that some types behave this way does not mean moves are destructive. They're still non-destructive. Indeed you're still supposed to call the destructor after a move, and the destructor very well _could_ do something in general. It's just that it doesn't happen to make any difference for typical implementations of some types like unique_ptr. 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.
- dragontamer 6y agoI guess I'm being imprecise with terminology then. It seems like Rust has a concept called destructive move, which is not what I'm talking about. In any case, the "foo" vector, and "foo" unique_ptr are _mutilated_ as part of the std::move operation. (Since the word "destroyed" seems to have been taken by the Rust community, I can't use that word anymore... hopefully no other community has taken the word "mutilated")
- gpderetta 6y agoI think "moved-from" already captures what you are trying to say. The theoretical destructive move is understood not leave an object behind at all, i.e. nothing to call a destructor on.