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

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 08: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-30-2006, 12:00 AM

Tags for this Thread