Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2003-11-28 05:39:28


David Abrahams wrote:
> Daryle Walker <darylew_at_[hidden]> writes:
>
>>All of the operator helpers need the final type to provide a core routine.
>>AFAIK, Peter's idiom is to use a pointer-to-member(-function) as the core
>>Boolean routine; it can't be provided by a library.
>
> What am I missing?
>
> template <class T>
> struct bool_testable
> {
> private:
> struct impl { int x; };
> typedef int impl::*safe_bool;
> public:
> operator safe_bool() const
> {
> return !*static_cast<T const*>(this) ? 0 : &impl::x;
> }
> };
>
> struct X : bool_testable<X>
> {
> bool operator!() const { return true; }

Now add

       operator int() const { return 0; }

and see what happens. It compiles, but is definitely not what the user
intended. "if(x)" calls "operator int()" now! The current bool_testable
doesn't have this problem. Also, the current implementation of
bool_testable expectes the user to provide operator bool(), it adds
safety (and operator!()), which is IMHO the most obvious, preferable
interface and matches the naming conventions of the operators library.

Regards, Daniel

-- 
Daniel Frey
aixigo AG - financial solutions & 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