I have the following code:
class MarchingEvent
{
...
};
typedef std::list< std::shared_ptr<MarchingEvent> > Segment;
Can I do:
void myFunction(std::shared_ptr<MarchingEvent> A)
{
Segment segment;
segment.push_back( A ); // <- Questionable line.
}
Will my smart pointer be correctly incremented when pushing A to segment?
Will my smart pointer be correctly incremented when pushing A to segment?
Yes. That's what std::shared_ptr
is supposed to do.
Note, however, that if you don't use the object A
after the call to push_back
, you might want to change the last line to
segment.push_back(std::move(A));
to move-construct the element in segment
instead of copying it - the copying has already been done upon entry of the function, because A
is passed by value.