Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Using a moved-from std::string value, or one of any standard library type, isn't undefined behavior.


(/me does some quick googling)

I guess it’s in an “unspecified” state after move, which I suppose is different from undefined. Still doesn’t take away from my main point which is that C++ has no problem allowing you to use a reference that has been moved from (which I’d wager is a mistake in very nearly 100% of cases.)


Isn't that the difference between move and copy? Copy means the original is still valid; move means it isn't.


Well, that’s just the rub: It’s up to the implementor of a given type to decide what happens to an object that’s been moved-from. They could do something silly like make a `print(std::string&&)` function which doesn’t actually do anything to the passed-in string, just prints it, but requires callers to `std::move` the arguments, even if it’s not actually moving anything.

To C++, there’s no such thing as “moving” per se, there’s just “rvalue references”, and a standard library macro called “std::move” which is really just a static_cast to an rvalue reference. The compiler doesn’t mind if you continue to use something after casting it via std::move. It’s all rather disappointing, really (although I can understand why they did it… backwards compatibility is important in the C++ world.)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: