Boost logo

Boost :

Subject: Re: [boost] [safebool] Can we generalize it and put it into utilities?
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2009-03-30 20:27:04


On Monday, March 30, 2009 7:09 PM
Vladimir Batov wrote:
>
> struct Foo
> { ...
> operator safebool<Foo>::type() const
> { return safebool<Foo>(condition); }
> };

I think "safe_bool" is the better spelling than "safebool" because "safe" and "bool" are separate words.

"type" is a good replacement for "result" since it is shorter, but don't call the member function pointer. That is, omit the parentheses:

   operator safe_bool<Foo>::type
   { return safe_bool<Foo>(condition); }

I don't care for the implicit conversion on which that relies.

> or
> operator safebool<Foo>::type() const
> { return safebool<Foo>::make(c); }

"make" should be spelled "apply." It's longer but more in keeping with the rest of Boost.

> or
> operator safebool<Foo>::type() const
> { return make_safebool<Foo>(c); }
>
> I still like the first one but the other two seemed strong
> contenders and I realize they might be marginally faster
> (in theory anyway).

There's certainly something to be said for avoiding the construction of a temporary in which to save the bool on which the implicit conversion operator depends. Everything is simpler:

   template <class T>
   struct safe_bool
   {
      typedef void (safe_bool::*type)() const;

      static type
      apply(bool _value)
      { return _value ? &safe_bool::_() : 0; }

      void
      _() const {};
   };

I took the liberty of renaming "true_" to "_" as the function exists only as a placeholder and shouldn't be used. You can make it private if you like, of course, but who would bother creating an instance of safe_bool<T> just to call the _ member function that does nothing?

make_safe_bool<Foo>(c) would be easier written make_safe_bool(this, c), with the compiler deducing Foo from this, but since the return type must already be spelled safe_bool<Foo>::type, then the function body can be spelled safe_bool<Foo>::apply(c) just as easily.

Usage is now:

   struct Foo
   {
      ...
      operator safe_bool<Foo>::type() const
      { return safe_bool<Foo>::apply(condition); }
      ...
   };

That looks clean and consistent.

> Documented the need, the usage and the solution.

The rationale for templatizing the code is good. I'm glad you included that. It might be good to link to http://www.artima.com/cppsource/safebool.html, Bjorn Karlsson's article on the subject, for background.

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
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, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk