Boost logo

Boost :

From: Val (zooropa_at_[hidden])
Date: 1999-12-27 02:02:28

"dave abrahams" <abraham-_at_[hidden]> wrote:
original article:
> Giora Unger wrote:

> > 5. In your view, how should a comparison to NULL be performed ?
> > I found out that either an explicit cast, e.g.: (ILinePtr)NULL,
> > or an isNull() method are the options. So I added isNull()
> > Did I miss anything ?
> Yes: use get() to retrieve the raw pointer and compare it with 0 (or
> if you prefer). You will probably be happier in the long run if you
> boost code as little as possible, or you'll be rolling your isNull
> back into the boost code whenever there is an update to boost. On the
> hand, if you add thread-safety to shared_ptr, please submit it to
boost so
> we can use it ;)

Well, this begs the question: why not add an operator bool() method to
shared_ptr and scoped_ptr?

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.

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...


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