Pointer Container Library

Overview

Boost.Pointer Container provides containers for holding heap-allocated objects in an exception-safe manner and with minimal overhead. The aim of the library is in particular to make OO programming easier in C++ by establishing a standard set of classes, methods and designs for dealing with OO specific problems


Motivation

Whenever a programmer wants to have a container of pointers to heap-allocated objects, there is usually only one exception-safe way: to make a container of smart pointers like boost::shared_ptr This approach is suboptimal if:

  1. the stored objects are not shared, but owned exclusively, or
  1. the overhead implied by smart pointers is inappropriate

This library therefore provides standard-like containers that are for storing heap-allocated or cloned objects (or in case of a map, the mapped object must be a heap-allocated or cloned object). For each of the standard containers there is a pointer container equivalent that takes ownership of the objects in an exception safe manner. In this respect the library is intended to solve the so-called polymorphic class problem.

The advantages of pointer containers are:

  1. Exception-safe pointer storage and manipulation.
  1. Notational convenience compared to the use of containers of pointers.
  1. Can be used for types that are neither Assignable nor Copy Constructible.
  1. No memory-overhead as containers of smart pointers can have (see [11] and [12]).
  1. Usually faster than using containers of smart pointers (see [11] and [12]).
  1. The interface is slightly changed towards the domain of pointers instead of relying on the normal value-based interface. For example, it is possible for pop_back() to return the removed element.
  1. Propagates constness s.t. one cannot modify the objects via a const_iterator.
  1. Built-in support for deep-copy semantics via the The Clobable Concept

The disadvantages are:

  1. Less flexible than containers of smart pointers like boost::shared_ptr

The user does not need this library when shared semantics are required.


Upgrading from Boost v. 1.33.*

If the user upgrades from one of these versions of Boost, then there has been one major interface change: map iterators now mimic iterators from std::map. Previously, the user may have written:

for( boost::ptr_map<std::string,T>::iterator i = m.begin(), e = m.end();
     i != e; ++i )
{
  std::cout << "key:" << i.key();
  std::cout << "value:" << *i;
  i->foo(); // call T::foo()
}

This needs to be converted into:

for( boost::ptr_map<std::string,T>::iterator i = m.begin(), e = m.end();
     i != e; ++i )
{
  std::cout << "key:" << i->first;
  std::cout << "value:" << *i->second;
  i->second->foo(); // call T::foo()
}

Apart from the above change, the library now also introduces

Also note that Boost.Assign introduces better support for pointer containers.


Acknowledgements

The following people have been very helpful:


References

[1] Matt Austern: "The Standard Librarian: Containers of Pointers" , C/C++ Users Journal Experts Forum.
[2] Bjarne Stroustrup, "The C++ Programming Language", Appendix E: "Standard-Library Exception Safety"
[3] Herb Sutter, "Exceptional C++".
[4] Herb Sutter, "More Exceptional C++".
[5] Kevlin Henney: "From Mechanism to Method: The Safe Stacking of Cats" , C++ Experts Forum, February 2002.
[6] Some of the few earlier attempts of pointer containers I have seen are the rather interesting NTL and the pointainer. As of this writing both libraries are not exceptions-safe and can leak.
[7] INTERNATIONAL STANDARD, Programming languages --- C++, ISO/IEC 14882, 1998. See section 23 in particular.
[8] C++ Standard Library Closed Issues List (Revision 27), Item 218, Algorithms do not use binary predicate objects for default comparisons.
[9] C++ Standard Library Active Issues List (Revision 27), Item 226, User supplied specializations or overloads of namespace std function templates.
[10] Harald Nowak, "A remove_if for vector", C/C++ Users Journal, July 2001.
[11] (1, 2) Boost smart pointer timings
[12] (1, 2) NTL: Array vs std::vector and boost::shared_ptr
[13] Kevlin Henney, Null Object, 2002.

©Thorsten Ottosen 2004-2006. Use, modification and distribution is subject to the Boost Software License, Version 1.0 (see http://www.boost.org/LICENSE_1_0.txt).

Top