Passing const shared_ptr& versus just shared_ptr as parameter

前端 未结 6 1036
误落风尘
误落风尘 2021-02-02 09:11

I\'ve been reading quite a number of discussions about performance issues when smart pointers are involved in an application. One of the frequent recommendations is to pass a sm

6条回答
  •  别跟我提以往
    2021-02-02 09:56

    I think its perfectly reasonable to pass by const & if the target function is synchronous and only makes use of the parameter during execution, and has no further need of it upon return. Here it is reasonable to save on the cost of increasing the reference count - as you don't really need the extra safety in these limited circumstances - provided you understand the implications and are sure the code is safe.

    This is as opposed to when the function needs to save the parameter (for example in a class member) for later re-reference.

提交回复
热议问题