Boost logo

Boost :

Subject: Re: [boost] safe-bool CRTP class
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-05-27 15:55:13


Krzysztof Czainski wrote:
> 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.

I see. Unfortunately, "boolable" just sounds horrible to me.

> > > 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.

Notice that I'm calling for the function to be configurable.

> (It should be possible to provide either one.)

Here I suggested that it should be possible to specify a function that indicates the object is valid *or* one that indicates the object is invalid.

> > 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?

The latter.

A policy class would permit installing code to call various derived class functions and to negate the result, if necessary. By default, you can specify a policy class that expects operator !().

That approach would give the default you're currently advocating and give users flexibility to use an existing function rather than create operator !(). That only makes sense if the library user has many classes with some particular function they can use in lieu of operator !() because otherwise, specializing the policy wouldn't be worth the trouble.

We have legacy code, for example, that specifies a virtual isValid() member function. A policy class that returns the result of calling derivate::isValid() would be just the ticket for that hierarchy.

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer using std::disclaimer;
Dev Tools & Components
Susquehanna International Group, LLP http://www.sig.com

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.


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