Boost logo

Boost :

Subject: Re: [boost] [safebool] Can we generalize it and put it into utilities?
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2009-03-29 18:19:43


Hi,
----- Original Message -----
From: <Vladimir.Batov_at_[hidden]>
To: <boost_at_[hidden]>
Cc: <Vladimir.Batov_at_[hidden]>
Sent: Sunday, March 29, 2009 11:18 PM
Subject: [boost] [safebool] Can we generalize it and put it into utilities?

>
> Repeating safe-bool scaffolding over and over again is a real nuisance. It
> is worse for the end users as not exactly everyone is intimately familiar
> with the issue and the safe-bool workaround. Can we generalize it and put
> something like the following into utilities?
>
> struct safebool
> {
> typedef void (safebool::*unspecified_bool_type)() const;
> typedef unspecified_bool_type result;
>
> explicit safebool(bool v) : value_(v) {}
>
> operator result() const { return value_ ? &safebool::internal_bool :
> 0; }
>
> private:
>
> void internal_bool() const {};
>
> bool value_;
> };
>
> That way all we (and the users) will have to do to deploy the technique
> will be
>
> class Foo
> { ...
> operator safebool::result() const { return safebool(my_condition); }
> };
>
> This seems simple and hassle-free.

Yes, this seems much more simple and open than the mixin approach.
Why do you use result instead of unspecified_bool_type. The following it enough clear to me

class Foo
{ ...
  operator safebool::unspecified_bool_type() const {
    return safebool(my_condition);
  }
};

I like it anyway,
Vicente


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