4 ms·
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.
by throwaway91111 9y ago
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.
- dbaupp 9y agounique_ptr requires an allocation whereas this does not, but it is using the same language features as unique_ptr.
- akubera 9y agoYeah, 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.