26

What are the equivalent uses of each smart pointer in comparison to similar (but not limited to) some advanced techniques using raw pointers?

My understanding is minimal, but from what I can gather:

  • Raw Pointers: Only use if you really, really, really, really, know what you are doing and have carefully hidden usage behind an interface.
  • std::auto_ptr: Obsolete never use.
  • std::unique_ptr: Singleton pointer that transfers ownership upon assignment.
  • std::shared_ptr: Reference counted pointer that does not transfer ownership upon assignment but increments its reference count. When all references leave scope or are explicitly std::shared_ptr::reset the underlying deallocator is called.
  • std::weak_ptr: A sub-type std::shared_ptr that does not increment the reference count and is invalidated when its parent std::shared_ptr no longer exists. May return and invalid reference. Always check before using.

RAW POINTER EQUIVALENT EXAMPLES

Reference counting, cache implementations: std::map<std::string, std::pair<long, BITMAP*> > _cache;

Singletons with transfer of ownership:

class Keyboard {
public:
//...
    static Keyboard* CreateKeyboard();
    ~Keyboard();
//...
private:
//...
    Keyboard();
    static Keyboard* _instance;
//...
};

Aggregate Containers, no ownership: Spatial partitioning graphs and trees, iterative containers, etc.

Composite Containers, ownership: Large objects.

--EDIT--

As I am working I came upon an interesting case, DeadMG pointed out that smart pointers are supposed to be used as easy abstractions to take care of resource management; what about file-scope objects that can not be created on the heap at the point of declaration but instead must be created at a later time?

Casey
  • 10,297
  • 11
  • 59
  • 88
  • 2
    I don't understand what you are asking. How to implement smart pointer behavior with raw pointers? – Neil Kirk Aug 16 '13 at 02:02
  • 1
    I have no idea what this question is, but if you're calling `unique_ptr` a Singleton pointer, I suspect you have a totally different meaning of Singleton here. – Puppy Aug 16 '13 at 02:06
  • @NeilKirk The exact opposite, actually, what are the uses of smart pointers compared to raw pointers? i.e. what idiom is each smart pointer supposed to replace? – Casey Aug 16 '13 at 02:17
  • 1
    @Casey: You might want to consider editing that into your question. – Puppy Aug 16 '13 at 02:20
  • @DeadMG From MSDN: `...unique_ptr uniquely manages a resource...A resource can be owned by no more than one unique_ptr object; when a unique_ptr object that owns a particular resource is destroyed, the resource is freed. A unique_ptr object may be moved, but not copied...`. Given the remarks, I took this to mean that there can only be one resource in existance. i.e. A Singleton. I may be wrong though, like I said, I have minimal understanding of how smart pointers work and are supposed to be used. – Casey Aug 16 '13 at 02:24
  • 1
    @Casey: No, there is only one unique_ptr *per resource*. And even that can be a bit flexible if you start to screw around with custom deleters that do things other than destroy the resource completely. – Puppy Aug 16 '13 at 02:36
  • @Casey Singleton usually refers to a global variable kind of thing. – Neil Kirk Aug 16 '13 at 02:40
  • 1
    @DeadMG Ohhh, so it's a one-to-one relationship instead of `std::shared_ptr`s one-to-many. – Casey Aug 16 '13 at 02:41
  • @Casey: Yes. Singleton is the "I'm just as shitty as a global variable and I add the shitty of only-one-of-my-type" crap. – Puppy Aug 16 '13 at 02:53

1 Answers1

5

what idiom is each smart pointer supposed to replace?

Every single one of them, ever, that eventually involved destroying the pointed-to resource. So in other words, virtually all of them. I can think of no idioms involving raw pointers that did not involve destroying a pointed-to resource. Every other use isn't really an idiom, it's just "Using a pointer".

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Aggregate containers don't destroy their pointed-to resources, they just remove them from a list. This is a chief concern that I had when studying smart pointers, which smart pointer would be the likely candidate for such a usage? – Casey Aug 16 '13 at 02:29
  • @Casey: If they don't destroy their pointed-to resources, why would they use a smart pointer at all? Smart pointers are for destroying things. – Puppy Aug 16 '13 at 02:34
  • Okay, this is why I was confused. Every person that advocated smart pointers that I'd read vehemently opposed the use of raw pointers in any form or fashion and should be replaced with a smart pointer. – Casey Aug 16 '13 at 02:44
  • 5
    @Casey: That is only for destroyable resources. Raw pointers are still essential *observers*, just like references. – Puppy Aug 16 '13 at 02:54
  • @DeadMG Aren't raw pointers, even as observers, outdated? Unless handling old code which uses `C` arrays, either a reference or an iterator would do the job. – VF1 May 18 '14 at 03:10
  • 2
    @VF1 You can't re-seat a reference. If the observer can ever be null using a reference is not going to work. – Casey Jun 01 '14 at 01:45