Purpose of const boost :: shared_ptr <T> & as function argument?

I am using some large and well-supported open source C ++ library and have come across a class definition having a form constructor

class SomeClass { SomeClass( const boost::shared_ptr<SomeOtherClass>& ); } 

My question is: what is the transmission point of const boost::shared_ptr<T> by reference? Is there really a really small amount of overhead associated with passing boost::shared_ptr<T> by value, or is there any other danger to passing boost::shared_ptr<T> by value that I don’t know about?

+4
source share
3 answers

Passing this value will copy it, which will lead to an increment in the reference counter, synchronized across all threads. Definitely not negligible.

+9
source

You not only avoid any (minor) overhead when copying shared_ptr , but also declare your intention not to copy the pointer. I do not think you can make a copy of shred_ptr without modifying it, and const prevent this.

+1
source

If you do not need a copy, why make a copy? That would be another question if they would still copy it in the body.

+1
source

Source: https://habr.com/ru/post/1410803/


All Articles