Boost logo

Boost :

From: Fernando Cacciola \(Home\) (fernando_cacciola_at_[hidden])
Date: 2003-02-20 08:36:33


Hi!

I'm under the process of getting rid of some of my old
smart pointers replacing them by shared_ptr<>.
There is however one idiomatic usage that it's pretty
hard to locate and edit, so I wondered if shared_ptr<>
could support it.
One is initialization from a null pointer value, as in:

struct C
{
  C() : ptr(0) {}

  shared_ptr<X> ptr ;
} ;

this one is very useful because its very idiomatic.

The other one, definitely not recommended but which *I* need
to support because my code if full of it,
is assignment of a null pointer value as a synonym for reset(),
as in:

struct C
{
  C() : ptr(0) {}

  void clear()
  {
    ptr = 0 ; // the same as ptr.reset();
  }

  shared_ptr<X> ptr ;
} ;

AFAICT, these can be _safely_ supported in shared_ptr<>
by simply adding these:

explicit shared_ptr( unspecified_bool_type const null_ptr_value ): px(0),
pn() // never throws in 1.30+
{
  BOOST_ASSERT( null_ptr_value == 0);
}
shared_ptr& operator = ( unspecified_bool_type const null_ptr_value )
{
  BOOST_ASSERT( null_ptr_value == 0);
  reset();
  return *this ;
}

I've tested it with bcc5.5.1 and the following holds:

T value ;
T* ptr = &value ;
shared_ptr<T> pa(ptr); // OK
shared_ptr<T> pb(0) ; // OK.
sahred_ptr<T> pc(1) ; // ERROR

pa = ptr ; // ERROR
pa = 0 ; // OK
pa = 1 ; // ERROR

What do you think?

Fernando Cacciola


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk