Boost logo

Boost :

Subject: Re: [boost] [safebool] Can we generalize it and put it into utilities?
From: Vladimir Batov (batov_at_[hidden])
Date: 2009-03-30 07:34:02

> From: "Steven Watanabe" <watanabesj_at_[hidden]>
> So, here's another take on it:
> template<class Tag>
> struct safe_bool {
> typedef void (safe_bool::*result_type)();
> result_type operator()(bool b) { return b? &safe_bool::true_ : 0 }
> private:
> void true_() {}
> };

Yes, "templatazing" makes sense. The deployment will probably be as follows:

class Foo
{ ...
    operator safe_bool<Foo>::result_type() const { return
safe_bool<Foo>()(my_condition); }

Is it considerably different from what I suggested initially? That is,

class Foo
{ ...
    operator safebool<Foo>::result() const { return
safebool<Foo>(my_condition); }

I do understand that my suggested variant stores the condition internally. I
did it for the sake of simplifying the user interface as I find the usage
immediately above fairly conventional. Does that variable adds too much
overhead? A way to avoid it certainly would be via a function similar to:

operator safebool<Foo>::result() const { return
safebool<Foo>::apply(my_condition); }
operator safebool<Foo>::result() const { return
safebool<Foo>()(my_condition); }


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