Consider the following vector:
std::vector<std::shared_ptr<X>> myVector;
and the following two functions that add a given element to the vector:
void foo1(std::shared_ptr<X> x)
{
myVector.push_back(x);
}
void foo2(const std::shared_ptr<X>& x)
{
myVector.push_back(x);
}
My understanding is that both functions push a shared_ptr
to X
into the vector and thus increment the ref count of X
. The first function results in an additional increment and decrement of the reference count but this is needless.
Is my understanding correct? Is the second option therefore preferable?