by staticassertion · 8y ago · 0 comments
In c++ you get copy semantics by default, requiring std::mov to get move semantics. In rust it is the opposite.
And even then:
1. std::move only creates an rvalue reference, it doesn't necessarily move anything (not only does that depend on the presence of a move ctor & the receiver's arguments, there's still no requirement that anything actually moves)
2. a move in the C++ sense "empties" the object, the object is still there and accessible in "some valid but otherwise indeterminate state", objects can even fully specify the exact state they're in when moved-from (unique_ptr does)
This is one area where Rust really makes life much easier.
well, take for instance this function :
my_struct eat_pointer(std::unique_ptr<int>&& x) {
if(x && *x > 0)
return my_struct{std::move(x)};
return my_struct{};
}
auto my_pointer = std::make_unique<int>(-456);
auto res = eat_pointer(std::move(my_pointer));
// at this point my_pointer hasn't moved a bit
sure, this code sucks, but that's always a possibility