Null Smart Pointers Using Boost

Managing memory is one of the pains that comes with developing in C++. Smart pointers such as std::auto_ptr help make things easier but they come with their own issues. My smart pointer implementation of choice is boost’s shared_ptr. A pattern I have found useful is:

   1  class Foo
   2  {
   3  public:
   4      typedef boost::shared_ptr<Foo> Ptr;
   5      static Foo::Ptr Null;
   6  public:
   7      // rest of declaration
   8  };

And then one can use them like:

   1  void f(Foo::Ptr foo)
   2  {
   3      if (foo == Foo::Null) 
   4      {
   5          // handle null case
   6      }
   7  }