News for hackers. Stuff that matters.
Re:You can't Rust that by the_mitsuhiko on Saturday March 31, 2018 @02:31PM (#16724464)
> I'm not really sure I understand what he's getting at with that description.

I'm getting at that an object in C++ is generally immovable. If it does move (which is new in C++11 i think?) a move ctor is invoked. Rust does not have a move ctor, it does not let you do things that would prevent moving an object in the first place (at least currently).

Re:You can't Rust that by gmueckl on Saturday March 31, 2018 @02:53PM (#16724559)
A move in C++ is a special construct where a new object is allocated in a new memory location and then constructed from the existing object in a way that must guarantee that the existing object is invalidated in some way. This must be handled explicitly in the implementation. For example, for std::vector this means that the move-constructed container steals the internal pointer to the data buffer from the old vector and resets the old vector to empty. This avoids a big and usually superfluous copy operation.
Re:You can't Rust that by masklinn on Saturday March 31, 2018 @02:59PM (#16724587)
> in a way that must guarantee that the existing object is invalidated in some way

That statement is confusing as the spec very specifically notes that the moved-from object remains valid, so saying that it's invalidated is odd.

Re:You can't Rust that by gmueckl on Saturday March 31, 2018 @03:06PM (#16724625)
You are confusing the now invalid internal state with the continuing existence of an accessible object at the old location. So you can move from an object and then access it. The standard guarantees that the object is still around. But because its data got stolen, its internal state must now be something invalid or empty depending on the semantics of the object.
Re:You can't Rust that by cornstalks on Saturday March 31, 2018 @04:21PM (#16724990)
You’re using way too strong of language. A move in C++ doesn’t require the original object to be invalidated or even modified in any way. POD-ish types, for example, are “moveable” despite not being made empty or invalid after a move (their move is implemented as a copy, which is perfectly valid).
Re:You can't Rust that by MereInterest on Saturday March 31, 2018 @04:46PM (#16725100)
As an example of this, in `boost::asio`, a moved-from socket is in a valid state, and is ready to accept a new connection.

https://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/re...

Re:You can't Rust that by masklinn on Saturday March 31, 2018 @04:55PM (#16725134)
> You are confusing the now invalid internal state with the continuing existence of an accessible object at the old location.

I'm not confusing anything, and the spec very explicitly notes that a moved-from object is in a "valid but unspecified state", not in an invalid state.

> The standard guarantees that the object is still around.

The standard's guarantees are significantly stronger than that, and furthermore

> But because its data got stolen, its internal state must now be something invalid or empty depending on the semantics of the object.

That is absolutely not a hard rule, a trivial move constructor is a copy constructor and does not affect the moved-from object in any way.

Re:You can't Rust that by wilun on Saturday March 31, 2018 @07:54PM (#16725823)
> "valid but unspecified state", not in an invalid state

That's mostly a detail because you can't rely on that to do anything interesting. Basically you can assign a new value to the object, or just destruct it. Having a "valid" but unknown value is completely useless.

What is sad is that the same mechanism has been overloaded for at least 2 different things: simple perf optim (when you fall into this situation of no guarantee), and movable unique ownership (like unique_ptr) where you actually can read the resulting object after the move an expect a predictable (in a portable and standardized way ) outcome.