Boost Pointer Container Library

The Boost Pointer Container library provides the equivalent functionality of each of the containers in the STL, but with the containers owning their objects through pointers rather then by making copies. As a result, the library allows exception safe code that is efficient and can use the object oriented design paradigm. This library is therefore suitable for handling collections of objects of types determined at run time but related through a polymorphic inheritance hierarchy.

The key features of this library are:

  • Holding pointers allows effective use of polymorphism (i.e., virtual functions)
  • The containers own the objects they have pointers to, i.e., the elements within the container, and will destroy them when they are themselves destroyed
  • Zero overhead both in terms of memory size and run-time access of elements
  • Lifetime of objects is easy to analyse and understand compared to boost::shared_pointer<>

Here is an illustrative example of constructing a portfolio of financial instruments:

#include <boost/ptr_container/ptr_vector.hpp>

// Abstract base class defining the interface of instruments to be
// priced
class Instrument
{
public:
  virtual ~Instrument() {};
  virtual double PV(void) const = 0;
};

// Two sample instruments
class VanillaEqOpt :
  public Instrument
{
  virtual double PV(void) const;
};

class VolSwapEqOpt :
  public Instrument
{
  virtual double PV(void) const;
};

class Portfolio
{
  typedef boost::ptr_vector<Instrument>  C;

  // All instruments get automatically destroyed when the Portfolio
  // object is destroyed
  C p;

public:

  void add(Instrument *i)
  {
    p.push_back(i);
  }

  double PV(void) const
  {
    double sum=0;
    for(C::const_iterator i=p.begin();
        i != p.end();
        ++i)
    {
      sum += i->PV();
    }
  }

};

In order to understand the advantages of the above approach lets consider the alternatives.

The simplest alternative is to use a container of naked pointer, i.e., std::vector<Instrument *> instead of boost::ptr_vector<Instrument>. The advantages of the pointer container approach are that:

  • It is not necessary to write a destructor

  • boost::ptr_vector<> automatically prevents copying, therefore eliminating a common source of errors:

    Portfolio p1;
    Portfolio p2(p1); // Error, can't copy
    
  • The syntax of accessing elements of boost::ptr_vector<> simpler since there isn't an extra layer of indirection:

    boost::ptr_vector<Instrument>::const_iterator i;
    i->PV();
    std::vector<Instrument *>::const_iterator j;
    (*j)->PV();
    

The other alternative is to use a std::vector<> containing elements of a smart pointer implementation such as boost::share_ptr<>. The advantages of the boost pointer containers over using boost::share_ptr<> are:

  • Better efficiency since reference counting is not required
  • Simpler syntax (i.e., one fewer layer of indirection as illustrated above)
  • The lifetime of elements is guaranteed to be the same as the lifetime of the container owning them, hence the code is easier to understand

Typical usage of this library computational finance would be (like illustrated) to hold a portfolio of instruments.