Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2003-02-25 08:18:31


> Essentially you supply a operator! and it supplies an unspecified-bool-type
> conversion operator.

Nice idea! I already had something similar in mind, but you managed to
make it really generic.

> //Key
> //T: primary operand type
> //t: values of type T
>
> // Template Supplied Operations Requirements
> //
> // bool_testable<T> operator unspecified-bool-type() !t
>
> template <class T, class B = ::boost::detail::empty_base> struct
> bool_testable : B
> {
> private:
> void safe_bool_conversion() const { }
> public:
>
> typedef void (bool_testable<T, B>::*unspecified_bool_type)() const;
> operator unspecified_bool_type() const
> {
> return !static_cast<const T&>(*this) ? 0 : &bool_testable<T,
> B>::safe_bool_conversion;
> }

The only problem I see is that an instance of safe_bool_conversion is
created which is not really needed. I suggest to rely on the operator!
provided by T:

template< class T, class B = ::boost::detail::empty_base >
struct bool_testable : B
{
private:
   typedef bool (T::*unspecified_bool_type)() const;

public:
   operator unspecified_bool_type() const
   {
      return !static_cast< const T& >( *this ) ? 0 : &T::operator!();
   }
};

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