Learning Hub Search Ctrl K

Legacy C++ › 7. Idioms and Patterns

Smart Pointers before C++11 (Boost)

scoped_ptr, shared_ptr and weak_ptr as used in the C++03 era.

intermediate #boost#scoped_ptr#shared_ptr#weak_ptr#smart-pointers#c++03

Explanation

Before C++11 the standard offered only std::auto_ptr, so real-world C++03 code used the Boost libraries (header-only for these types), later standardized by TR1 and then C++11. If you maintain old code you will see them.

Boost type (header <boost/...>) Semantics Became
scoped_ptr<T> sole owner, non-copyable, no transfer unique_ptr (non-moved use)
scoped_array<T> same, for new[] unique_ptr<T[]>
shared_ptr<T> reference counted shared ownership, copyable, works in containers std::shared_ptr
weak_ptr<T> non-owning observer, breaks reference cycles std::weak_ptr
intrusive_ptr<T> counter lives inside the object (no equivalent)

In MSVC 2008 SP1+ and GCC 4.x the same names are available as std::tr1::shared_ptr.

Example

This needs the Boost headers, so it will not run in Compiler Explorer’s default setup. It is shown as a snippet.

#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <iostream>
#include <vector>

struct Node {
    boost::weak_ptr<Node> parent;           // weak: avoids a cycle with children
    std::vector< boost::shared_ptr<Node> > children;
    ~Node() { std::cout << "Node destroyed\n"; }
};

void demo() {
    boost::scoped_ptr<Node> local(new Node);          // freed at end of scope, even on throw
    // boost::scoped_ptr<Node> copy = local;          // error: non-copyable (unlike auto_ptr)

    boost::shared_ptr<Node> root(new Node);
    boost::shared_ptr<Node> child(new Node);
    child->parent = root;                             // weak back-reference
    root->children.push_back(child);                  // shared_ptr works in std containers

    std::cout << root.use_count() << " " << child.use_count() << "\n";   // 1 2
    if (boost::shared_ptr<Node> p = child->parent.lock())                // safe access
        std::cout << "parent alive\n";
}   // everything is destroyed exactly once

// call demo() from main()

Common pitfalls

  • Cycles between shared_ptrs leak (A owns B, B owns A). Break one direction with weak_ptr.
  • Never create two independent shared_ptrs from the same raw pointer: each gets its own counter and the object is deleted twice.
  • Creating one inside a function call with other throwing arguments can leak; use boost::make_shared (the equivalent of std::make_shared).
  • shared_ptr has overhead (control block, atomic counter updates); use scoped_ptr/references when sharing is not needed.
  • A shared_ptr is not thread-safe for concurrent modification of the same pointer object, only for the reference count.

Legacy vs modern

In C++11 and later, use std::unique_ptr, std::shared_ptr and std::weak_ptr, with make_unique (C++14) and make_shared. Migration is mostly renaming boost:: to std::. See shared_ptr and weak_ptr.

Exercises

1. Which Boost type replaces auto_ptr when you do not want ownership transfer?

boost::scoped_ptr: it simply refuses to be copied, so mistakes are compile errors.