Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2002-11-04 14:00:17


Peter Dimov wrote:
>
> From: "Daniel Frey" <daniel.frey_at_[hidden]>
>
> > What is wrong with:
> >
> > class A
> > {
> > private:
> > operator int() const;
> >
> > public:
> > operator bool() const { return ...; }
> >
> > };
>
> Nothing, I think. Who invented it?

I certainly wasn't the first and I wouldn't even dare to use the word
"invent" for it :) But your code produces much better error messages,
thus I prefer it. How about generalizing it? Something like:

#define BOOST_SAFE_BOOL( this_t ) \
private: \
  typedef bool (this_t::*unspecified_bool_type)() const; \
public: \
  operator unspecified_bool_type() const \
  { return !(*this) ? 0 : &this_t::operator!; }

class X
{

public:
  bool operator!() const {
    // The user's logic goes here!
  }

  BOOST_SAFE_BOOL( X );

};

Maybe we should remove the private/public stuff, as it may lead to some
confusion and the typedef isn't really hurting...

Regards, Daniel

--
Daniel Frey
aixigo AG - financial training, research and technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey_at_[hidden], web: http://www.aixigo.de

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