Boost logo

Boost Users :

From: Miko (miko_at_[hidden])
Date: 2003-01-29 14:53:40


>From: "David Abrahams" <dave_at_[hidden]>

> "wasekvesely <vaclav.vesely_at_[hidden]>" <vaclav.vesely_at_[hidden]> writes:
>
> > I've found this in "shared_ptr.hpp":
> >
> > // implicit conversion to "bool"
> > typedef T * (this_type::*unspecified_bool_type)() const;
> >
> > operator unspecified_bool_type() const // never throws
> > { return px == 0? 0: &this_type::get; }
> >
> > Why this is better than:
> >
> > operator bool() const
> > { return px != 0; }
> >
> > What more can I do with somethin like this?
>
> It's not that you can do more; quite the opposite.
>
> int f(long);
> int x = f(shared_ptr<int>()); // error
>
> With operator bool(), that (and many other abuses) would compile.

Yeah. It's the same reason that stream classes has an implicit conversion to
pointer, to give a bool value regarding its state. With an implicit
conversion to bool, you could have done nonsensical things like the above
example, and e.g.:

int value=cin+cin; // Would compile!

Instead, using the current definition, you may do:

delete cin; // (!)

This is fixed in shared_ptr, though, so you can't do that, either.

Regards,

Terje


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net