Thread: 'good' smart pointer for factory?

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    'good' smart pointer for factory?

    What is a good boost smart pointer to return from a factory?

    shared_ptr or scoped_ptr?

    I read that shared_ptr is only used in certain circumstances like when putting the pointer into STL containers. But scoped_ptr seems too simple for generic use.

    Is there a 'best' smart pointer for generic use?

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Scoped cannot be returned, because it has no copy semantics. I would say that scoped_ptr is used only in certain circumstances, not shared_ptr, which is used widely and should be preferred over scoped.

    There is no the best one.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    At the moment, I think it is std::tr1::shared_ptr. But then std::unique_ptr might be better in general, when the next version of the C++ standard is finalised.
    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

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I'll step out on a limb and say that you should return a "dumb" pointer. The factory pattern is usually used in VERY loosely coupled code. Returning any kind of smart pointer will force the caller to use that smart pointer type in his own code. He might not want to.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by kmdv View Post
    Scoped cannot be returned, because it has no copy semantics. I would say that scoped_ptr is used only in certain circumstances, not shared_ptr, which is used widely and should be preferred over scoped.

    There is no the best one.
    I read about this in the google guidelines

    http://google-styleguide.googlecode....k/cppguide.xml


    "If you actually need pointer semantics, scoped_ptr is great. You should only use std::tr1::shared_ptr under very specific conditions, such as when objects need to be held by STL containers. You should never use auto_ptr."

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by KIBO View Post
    I read about this in the google guidelines

    http://google-styleguide.googlecode....k/cppguide.xml


    "If you actually need pointer semantics, scoped_ptr is great. You should only use std::tr1::shared_ptr under very specific conditions, such as when objects need to be held by STL containers. You should never use auto_ptr."
    Actually, you should only use boost::scoped_ptr and std::tr1::shared_under very specific conditions: for the former, when you need a smart pointer with a lifetime within some scope; for the latter, when you need a smart pointer with shared semantics.

    std::unique_ptr should be used when you need a smart pointer with unique ownership of the object. It is also useful when you need a smart pointer with a lifetime within some scope, since scoped_ptr is and will remain non-standard (at least in the near future).

    In this sense, brewbuck's point is a good one. Yet, I would rather return std::unique_ptr since it has minimal overhead and still allows the caller to convert it to whatever other (smart) pointer type is needed, with the peace of mind that RAII is safely used otherwise.
    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

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Hm, in my expierience I noticed, that whenever I attempted to use a non-copyable smart pointer, sooner or later I missed that feature and got back to the shared. From my point of view, they are useless as data members because they make the whole class non-copyable. And when it comes to buffers, I use STL container or bare instance (what is point of allocating a single object on heap?), or an own raw vector class for raw data. The only case I would use it would be to make it a result variable. But again, if I return it, I probably copy it somewhere else too.

    So my unique_ptr usage is downgraded to the level I do not use it at all.

    And I might be wrong, but weak_ptr can't be used with scoped_ptr / unique_ptr?
    Last edited by kmdv; 11-04-2010 at 11:48 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kmdv
    And I might be wrong, but weak_ptr can't be used with scoped_ptr / unique_ptr?
    Yes, it is meant to work with shared_ptr.
    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

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Thanks for info, I did not know about unique_ptr that seems to a 'best' choice for returning from a factory indeed withe the most freedom for client programmers

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by kmdv View Post
    Hm, in my expierience I noticed, that whenever I attempted to use a non-copyable smart pointer, sooner or later I missed that feature and got back to the shared. From my point of view, they are useless as data members because they make the whole class non-copyable. And when it comes to buffers, I use STL container or bare instance (what is point of allocating a single object on heap?), or an own raw vector class for raw data. The only case I would use it would be to make it a result variable. But again, if I return it, I probably copy it somewhere else too.

    So my unique_ptr usage is downgraded to the level I do not use it at all.

    And I might be wrong, but weak_ptr can't be used with scoped_ptr / unique_ptr?
    If you find later that the non-copyable smart pointer is limiting you, then it's probably a sign that your design has flaws in one or another way. Either you did not anticipate what might be required in the design, or it turns out the design simply doesn't work, or you made a mistake.
    The smart pointer is usually supposed to catch such mistakes by having proper semantics.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by KIBO View Post
    What is a good boost smart pointer to return from a factory?
    Hmm, it seems you've put half the answer in the question.
    What's wrong with a lowly auto_ptr? The caller is free to take that data and put it into something else.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by iMalc
    What's wrong with a lowly auto_ptr?
    It will be deprecated in the next version of the C++ standard in favour of unique_ptr, so I am reluctant to suggest it as part of an interface.
    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

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I'm throwing my vote in for the suggestion offered by brewbuck.

    Why burden a factory with a specific smart pointer when your client could very easily stick it in any reasonable smart pointer after the fact.

    Soma

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by laserlight View Post
    It will be deprecated in the next version of the C++ standard in favour of unique_ptr, so I am reluctant to suggest it as part of an interface.
    Oh wow, I didn't know that.

    Yeah being a factory method I'm not entirely convinced there is a need to return a smart pointer either. I mean new itself doesn't give you a smart pointer, you have to put it into something. This situation isn't much different.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. pointer and smart pointer address
    By l2u in forum C++ Programming
    Replies: 14
    Last Post: 12-26-2006, 05:00 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM