• Quick reference of C++ value categories: Part 2
    0
    As I have said, downsides can be unimportant in your code. Decision has to be made for each particular part of code where you use such an approach.
  • Quick reference of C++ value categories: Part 2
    0
    Hi. Could you please use english language in comments, so that both the post and the discussion are useful to international audience. So, the question is: should we pass shared_ptr by value or create a local copy and dereference it. I added some links with the source of this recommendation so that you can get more detailed information about it.

    Passing shared_ptr by value obviously looks more simple and «comfortable» as you say in this simple example, but it also has downsides that I can think of, which may be important or not important in your code:
    — you are passing to the function more than it needs (if it just needs a reference) and thus we incapsulate additional logic into that function, making it less general and less eligible for reuse. This function cannot be used for a non-shared_ptr
    — you will have to make copy of shared_ptr with every call of this function if you will need to call it multiple times
    — you will have to make copy of shared_ptr with every call of other functions in this block if you follow this approach

    Also, please note that recommendation is to use auto instead of shared_ptr — you can find out why this is important by following the same link to youtube video.