Boost logo

Boost :

From: Greg Colvin (gcolvin_at_[hidden])
Date: 1999-12-27 04:06:36

From: Val <zooropa_at_[hidden]>
> ...
> Well, this begs the question: why not add an operator bool() method to
> shared_ptr and scoped_ptr?

If you want a conversion to element_type* you can define
BOOST_SMART_PTR_CONVERSION. I'd prefer it be the default,
but not enough to argue with those who didn't. So you
you don't need a conversion to bool.
> If the goal is to "mimic a built-in pointer", then an operator bool()
> conversion would make sense. It seems that using get() should be
> avoided whenever possible, as it could be dangerous to let people play
> with the _real_ pointer as long as reference-counted version exists.

Some people fear pointers, some don't.

> Also, if you want to make templatized algorithms that can work with
> smart pointers or real pointers, then having to do "if (ptr.get() ==
> NULL)" isn't going to be nearly as convenient. You can of course get
> around that, using template specialization for smart_ptr types, or
> doing a static cast of NULL so that the explicit constructor will get
> called and operator== used, but operator bool() seems much more elegant
> and direct...

So define BOOST_SMART_PTR_CONVERSION and use "if(!p)",
else use "if(!&*p)".

Boost list run by bdawes at, gregod at, cpdaniel at, john at