Passing std:: or boost::shared_ptr as a reference to a function

This is a discussion on Passing std:: or boost::shared_ptr as a reference to a function within the C++ Programming forums, part of the General Programming Boards category; I was suggested to replace in few places passing const shared_ptr by value to passing by reference (e.g. change const ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Location
    Kiev, UA
    Posts
    2

    Passing std:: or boost::shared_ptr as a reference to a function

    I was suggested to replace in few places passing const shared_ptr by value to passing by reference (e.g. change const std::shared_ptr param to const std::shared_ptr& param) when I was checking my project with PVS-Studio, the static code analysis tool.

    I understand that for performance hot-spots it might be a good optimization as creating a temporary copy of shared_ptr requires updating reference counter which is a interlocked operation.

    So the question is - how safe is this optimization? Are there any cases when it could not be used?

    Any thoughts on this would be appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    This has been answered elsewhere.

    I can't for the life of me understand why you would start a new thread in a different online community for a question that was already answered for you. Were the answers that you were given not up to standard? Even so, you should have asked for clarification, and only seek help elsewhere after it becomes clear that you are simply making no progress getting a good answer in that online community.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    Kiev, UA
    Posts
    2
    Quote Originally Posted by laserlight View Post
    This has been answered elsewhere.

    I can't for the life of me understand why you would start a new thread in a different online community for a question that was already answered for you. Were the answers that you were given not up to standard? Even so, you should have asked for clarification, and only seek help elsewhere after it becomes clear that you are simply making no progress getting a good answer in that online community.
    These communities are different, and I'm trying to get ideas and thoughts from as many people as possible.
    As I see there is no single opinion yet on this subject, I'd like to get to the bottom of the problem to be able then to start preaching proper solution.

    Also the link you have mentioned is a discussion, there is no final answer (hopefully - yet)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Boost shared_ptr and shared_array
    By MarkZWEERS in forum C++ Programming
    Replies: 9
    Last Post: 09-26-2008, 05:08 AM
  2. Boost Serialization: shared_ptr
    By KessiMC in forum C++ Programming
    Replies: 0
    Last Post: 12-26-2007, 07:17 PM
  3. How slow is boost::shared_ptr?
    By pheres in forum C++ Programming
    Replies: 11
    Last Post: 04-21-2007, 04:36 AM
  4. boost shared_ptr problem
    By l2u in forum C++ Programming
    Replies: 5
    Last Post: 09-11-2006, 09:02 AM
  5. boost::shared_ptr
    By IfYouSaySo in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2006, 11:00 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21