Boost logo

Boost :

From: Ed Brey (brey_at_[hidden])
Date: 2002-07-15 09:18:50

"Peter Dimov" <pdimov_at_[hidden]> wrote in message news:002f01c22bfe$fa97b8d0$1d00a8c0_at_pdimov2...
> I just want to point out that the shared_ptr conversion to bool is not
> merely syntactic sugar. It allows shared_ptr declarations in conditions:
> if(shared_ptr<X> px = make_shared(wp))
> {
> }

which by magic of the C++ language is syntatic sugar for:

  shared_ptr<X> px = make_shared(wp);
  if (px) {

:-) I'm not arguing with the usefulness, of course. I also find the declaration in conditions quite handy compared to their verbose alternatives.

> It'd probably be a good idea to add the conversion to scoped_* and
> shared_array for consistency (where it _is_ a syntactic sugar. :-) )

Likewise for operator! and operator ->*, if the latter turns out to be viable.

Along these lines, something I've toyed with is a boost::null, which would be syntacticly convenient. It looks like this:

namespace boost {
  struct null_t {
    template<typename T> operator T*() {return 0;}
  null_t const null;
  template<typename T> inline bool operator==(scoped_ptr<T> const& p, null_t)
    {return p.get() == 0;}

This would allow code like this:

using boost::null;
shared_ptr<X> px;
if (px == null) ...

To fill this out, there would be definitions for operator!=, plus reversal of the order of parameters, and all the smart pointers, including std::auto_ptr, would be included. Also, perhaps a pointer to member conversion would be needed for null_t.

It seemed like a neat idea (I'm pretty sure it wasn't mine - I think I read about it a long time ago), but MSVC7 doesn't handle it well at all, so I have never looked to see whether it is really a viable option.

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