Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 Smart Pointer Policies

As I understand it, in the current specification of C++11, one should use:

  • std::unique_ptr<> for one owner (most of the time)
  • std::shared_ptr<> only when there are multiple owners in acyclic structure
  • std::weak_ptr<> sparingly only when there are cycles that need to be broken
  • A raw pointer as a handle to memory (no ownership) when a reference would not suffice

So my questions are:

  1. Are these policies sufficient or are there additional policies that I should be aware of?
  2. Are scoped_ptr<> and auto_ptr<> effectively obsolete?
like image 593
kfmfe04 Avatar asked Dec 01 '11 00:12

kfmfe04


People also ask

Does C ++ 11 have smart pointers?

Introduction of Smart PointersC++11 comes up with its own mechanism that's Smart Pointer. When the object is destroyed it frees the memory as well. So, we don't need to delete it as Smart Pointer does will handle it. A Smart Pointer is a wrapper class over a pointer with an operator like * and -> overloaded.

How do you pass a smart pointer to a function?

You can't copy a unique_ptr at all. The whole point of the class is that it cannot be copied; if it could, then it wouldn't be a unique (ie: singular) pointer to an object. You have to either pass it by some form of reference or by moving it. Smart pointers are all about ownership of what they point to.

Should I use unique_ptr or shared_ptr?

Use unique_ptr when you want a single pointer to an object that will be reclaimed when that single pointer is destroyed. Use shared_ptr when you want multiple pointers to the same resource.

What is a smart pointer in C?

Smart pointer that enforces unique ownership by transferring ownership on copy. Comparable to the deprecated std::auto_ptr Class. CHeapPtr Class. Smart pointer for objects that are allocated by using the C malloc function.


1 Answers

Are scoped_ptr<> and auto_ptr<> effectively obsolete?

auto_ptr is deprecated in C++11, so there's your answer. scoped_ptr doesn't exist in C++11 and never did. The main reason to use boost::scoped_ptr is to ensure that ownership is never transferred (unless you cheat, of course). Then again, if you use unique_ptr, ownership can only be transferred if you use std::move or similar constructs. Or, as Howard points out, just make it a const std::unique_ptr.

So it's really up to you whether you want that extra bit of insurance. Also boost::scoped_ptr doesn't have deleter support. So unique_ptr can play tricks that boost::scoped_ptr cannot.

std::weak_ptr<> sparingly only when there are cycles that need to be broken

I can't say I agree with this policy necessarily. A weak_ptr should be used when an object may want to talk to something else, but it doesn't own that something else. Which means that it can be deleted at any time, and the holder of the weak_ptr needs to be able to handle that deletion anytime it tries to talk to it.

Breaking cycles is one of the uses of weak_ptr; it should not be the only time it is used.

like image 153
Nicol Bolas Avatar answered Sep 28 '22 01:09

Nicol Bolas