indirect_value icon indicating copy to clipboard operation
indirect_value copied to clipboard

Can I replace shared_ptr by indirect_value ?

Open bansan85 opened this issue 1 year ago • 1 comments

I can see that unique_ptr can be replaced by indirect_value. But what about shared_ptr ?

shared_ptr deserves to keep const-ness 😃 I tried propagate_const few years ago and const-ness worked for both of them.

bansan85 avatar Aug 04 '22 00:08 bansan85

Yes, indirect_value can replace unique_ptr and provides the additional copying and const propagation behaviour required to model value semantics. shared_ptr has very different semantics already, and the reference counting of shared instances is too far away from value semantics for indirect_value to be applicable.

In this case, if you have a member variable of shared_ptr<T> then it should probably be a propagate_const<shared_ptr<T>> but there are also proposals floating around for a new keyword propconst which if accepted would supersede this approach: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1974r0.pdf

Twon avatar Aug 05 '22 16:08 Twon

Thanks for the comment. Maybe an note about shared_ptr should be added in the README on somewhere else in the document ?

bansan85 avatar Aug 07 '22 15:08 bansan85