Boost logo

Boost :

Subject: Re: [boost] safe-bool CRTP class
From: Krzysztof Czainski (1czajnik_at_[hidden])
Date: 2011-05-27 13:36:09


2011/5/27 Stewart, Robert <Robert.Stewart_at_[hidden]>

> Krzysztof Czainski wrote:
>
> class boolable : public Base
>
> Why not s/boolable/safe_bool/?
>

I was trying to find a name than would fit nice into the operators library,
together with andable for example. Under the name boolable I understand it
may be tested for true/false like
if ( x ) ...
if ( !x ) ...
But I am not against changing the name.

>
> > {
> > void unspecified_bool_t_f() {}
> >
> > typedef void (boolable::*unspecified_bool_t)();
> >
> > public:
> >
> > operator unspecified_bool_t() const
> > {
> > return ! static_cast<Derived const&>( *this ) ? 0 :
> > &boolable::unspecified_bool_t_f;
>
> This doesn't support types that have no other reason to define operator !()
> or that have a ready-made is_valid() sort of function. IOW, the "valid" or
> "invalid" function on which to rely should be configurable. (It should be
> possible to provide either one.)
>
> I understand that you're assuming that if one can write if (x), one might
> assume that if (!x) should also work, but that should work anyway with the
> safe-bool operator, right?
>

Right. So the question is what function should the user provide? I proposed
operator!. Other options would be is_valid/is_invalid, to_bool . Or should
this function's name be customizable?

My choice was to fix this function as operator!, so as not to add anything
to the interface. It's like Robert says, "if (!x)" would work anyway even
without operator! defined.

Regards
Kris


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