Re:Emulating the Rust borrow checker with C++ move-only types
by throwaway91111 on Saturday May 13, 2017 @04:43PM
(#14332907)
Isn't this just a UniquePtr? Ie it allows moves, but references to it are not guaranteed to be valid. The latter is the borrow checker.
[ Parent
| on HN ]
Re:Emulating the Rust borrow checker with C++ move-only types
by akubera on Saturday May 13, 2017 @06:42PM
(#14333385)
Yeah, the unique_ptr<T> enforces move semantics and can be "borrowed" via get() (but unlike rust, further borrowing/moving rules are not enforced by C++). This post is an explanation of how to write your own class which enforces this for objects whether or not they are on the heap.
It's a good question to ask whether implementing a move-only class is more beneficial than wrapping a normal class in a unique_ptr. This comes down to trusting the programmer to know when to copy/move/delete (for better or worse).
The best policy is probably to implement ALL constructor/assignments so there's minimal frustration when, say, trying to write a subclass, and have a special subclass `UniqueFoo` that deletes copy methods to enforce Moveability - minimizing surprise and maximizing flexibility.