Source
- URL: https://x.com/SebAaltonen/status/930214265750945794
- Author: Sebastian Aaltonen (@SebAaltonen)
- Posted: 2017-11-13 23:22:18
Thread
1/ @SebAaltonen
@kingsamj @EricLengyel
Documented ownership is one of the main advantages of unique_ptr over raw ptr. Straightforward to understand who owns this object. Downside of unique_ptr is that destructor call needs a complete type. Forward declare isn’t enough.
2/ @SimonMoos
@SebAaltonen @kingsamj @EricLengyel
Do you then use raw pointers to indicate lack of ownership?
3/ @ScottMcMillan0
@SimonMoos @SebAaltonen @kingsamj @EricLengyel
That is the convention I’ve generally employed. I.e. never pass raw with a comment like ‘this takes ownership’, pass as unique_ptr if that’s the case. Avoid shared_ptr.
4/ @SebAaltonen
@ScottMcMillan0 @SimonMoos @kingsamj @EricLengyel
Passing unique pointer as an function argument (or returning it) always means that you are transferring ownership (factory function or sink). In common case you pass data by value or reference, unless null is a valid input for function (then use raw pointer).
5/ @SebAaltonen
@ScottMcMillan0 @SimonMoos @kingsamj @EricLengyel
When raw ptr is stored to a structure, it means that this object doesn’t own the memory. In a well designed system, an object with a raw ptr never outlives the data that it points to. For example iterators of a container use raw pointers to keep track of their location.
6/ @SebAaltonen
@ScottMcMillan0 @SimonMoos @kingsamj @EricLengyel
Also worth noting that often unique_ptr