Re:Under the hood of C++ lambdas and std::function
by bjz_ on Monday March 28, 2016 @01:17AM
(#11372852)
Rust's moves are implicit, making it easier to write code that doesn't copy heap allocated memory, and you can't get 'use after moves' like in C++ - the type system will pick that up.
[ Parent
| on HN ]
Re:Under the hood of C++ lambdas and std::function
by rjeli on Monday March 28, 2016 @01:42AM
(#11372903)
Oh, having only read about and not used recent C++ practices, I assumed use-after-move would be caught at compile time. Looking around a bit, it seems there's not even a warning for this; is there a reason? It seems like it would be very useful.
Re:Under the hood of C++ lambdas and std::function
by catnaroek on Monday March 28, 2016 @04:03AM
(#11373205)
In C++, "moving" and "copying" are a matter of class design. While you are expected to use move (resp. copy) constructors and assignments to, well, move (resp. copy); the language doesn't rule out using them for other purposes.
In Rust, assignment and argument passing always move, "moving" always does the right thing (make a shallow copy and invalidate the original object), and you can't override this behavior. Furthermore, Rust splits what C++ calls "copying" into two concepts: shallow copying (which is the same as moving, except the original object isn't invalidated, and the type checker guarantees you can only do it when it makes sense) and deep cloning (which may be expensive and needs to be explicitly requested by the programmer).
This is why Rust can track which objects are no longer usable.
Re:Under the hood of C++ lambdas and std::function
by Animats on Monday March 28, 2016 @02:54AM
(#11373071)
Move is a template level concept, not a language concept. The compiler has no idea that something was moved, rather than assigned. Some implementations null out the "moved from" reference, so that a use after move will dereference null and crash the program.
I've referred to the attempts to fix C++ ownership semantics with templates as "wallpapering over the mold".