The CppCoreGuidlines rule R.33 suggests to
Take a
unique_ptr<widget>&
parameter to express that a function reseats the widget.Reason Using
unique_ptr
in this way both documents and enforces the function call’s reseating semantics.Note “reseat” means “making a pointer or a smart pointer refer to a different object.”
I don't understand why we should pass by reference when reseat means "making a pointer or a smart pointer refer to a different object.”
When the function's purpose is to reseat/change the underlying object the pointer is pointing to, aren't we stealing the ownership from the caller this way and therefore should pass the unique_ptr
by value, hence moving it and transferring ownership?
Is there an example that explains why passing a unique_ptr
by reference is recommended?
Aucun commentaire:
Enregistrer un commentaire