4 ms·
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
by akubera 9y ago
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.