I want to pass an object by smart-pointer reference to a function. The function may change the value of the referenced object, but may not change the reference itself. There are two obvious ways to handle this.
The first approach to pass the shared_ptr by value - it is the reference, so doesn't itself need to be passed by reference. The apparent issue with this is copying of the reference, which suggests some reference-counting overhead.
void foo (shared_ptr<bar> p)
The second approach is to pass the shared_ptr by const reference - avoiding the copying of the shared_ptr instance, but instead implying that accesses to the referenced object need two layers of dereferencing instead of one.
void foo (const shared_ptr<bar> &p)
In practice, these theoretical overheads will normally be trivial and irrelevant. Which suggests to me that instead of choosing one approach or the other for each individual case, I should almost always follow some standard convention. Which leads to the question...
Is there a standard convention for which of these approaches I should normally choose? If so, which is the conventional choice?
EDIT - Probably worth mentioning - one reason to consider the pass-by-const-reference case is because there's a pre-existing convention that most class/struct instances are passed by const-reference rather than by value, and shared_ptr
is a class. Of course it isn't a heavyweight class (the cost of copying is small), so the reasons behind that older convention may not apply.