indirect_value
indirect_value copied to clipboard
Can I replace shared_ptr by indirect_value ?
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.
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
Thanks for the comment. Maybe an note about shared_ptr should be added in the README on somewhere else in the document ?