Pointer Containers
by Thorsten Ottosen 

Listing One
(a)
typedef vector< boost:: shared_ptr<T> > ptr_vector;

(b)
template< class T >
class ptr_vector
{
    std::vector<T*> vec_;
public:
    ~ptr_vector(); // delete objects
    // ... much more
   typedef <something> iterator;
};


Listing Two

(a)
void  push_back( T* );
template< class I >
void  insert( iterator before, I first, I last );

(b)
void push_back( T* t )
{ vec_.push_back( t ); }

(c)
void push_back( T* t )
{
  std::auto_ptr<T> p( t );
  vec_.push_back( t );
  p.release();
}


Listing Three

(a)
void insert( iterator before, I first, I last )
template< class I >
{
  while( first != last )
  {
      vec_.insert( before, new T( *first ) );
      ++first;
  }
}

(b)
void insert( iterator before, I first, I last )
template< class I >
{
 while( first != last )
 {
  auto_ptr<T> p(new T( *first ));
  before = vec_.insert( before, p.get() );
  ++before; // to preserve order
  ++first;
  p.release();
 }
}


Listing Four

void insert( iterator before, I first, I last )
{
 size_t n = distance(first, last);
 scoped_deleter<T> sd( n );
 for( ; first != last; ++first )
     sd.add( new T( *first ) ) );
 vec_.insert( before, sd.begin(), sd.end() );
 sd.release(); // cannot throw
}


Listing Five

typedef boost::indirect_iterator<ptr_iterator>::type iterator;
typedef boost::indirect_iterator<ptr_iterator> iterator;/
ptr_iterator ptr_begin();
ptr_iterator ptr_end();
iterator     begin();
iterator     end();


Listing Six

(a)
( *v.begin() )->foo();
*v.front() = X;
v[4]       = &X; // doh!
v[3]       = new X; // ooops!

(b)
v.begin()->foo();.
v.front()  = X;
v[3]       = &X; // compile error

(c)
vector<int>     v;
ptr_vector<int> v2;
 ...
std::copy( v.begin(), v.end(), std::back_inserter( v2 ) );


Listing Seven

// primary template:
template< class T >
T* allocate_clone( const T& t )
{ return new T( t ); }

// overloaded function template
template< class T >
X<T>* allocate_clone( const X<T>& t )
{ return factory::clone( t ); }
// function template specialization
template<>
Polymorphic* allocate_clone<Polymorphic >( const Polymorphic& p )
{ return p->clone(); }


Listing Eight

(a)
auto_ptr<T> release_back();
auto_ptr<T> release( iterator );

(b)
auto_ptr<ptr_vector<T> > clone() const;
auto_ptr<ptr_vector<T> > release();
ptr_vector( auto_ptr<ptr_vector<T> > );

(c)
void replace( iterator where, T* x );
void replace( size_type idx, T* x );
Listing Nine
template< class I >
void transfer( iterator before, I first, I last, ptr_vector<T>& from );
void transfer( iterator before, I i, ptr_vector<T>& from );





