Boost logo

Boost :

Subject: Re: [boost] safe-bool CRTP class
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-05-31 09:09:28

Bryce Lelbach wrote:
> Attached is a modified version of this that incorporates
> portability code from the smart_ptr version into the variant
> of the spirit safe_bool that Hartmut and I use at work.
> I like to have a macro for this, so I have an operator that's
> convertible to the safe_bool class, which is itself
> convertible to bool (this avoids problem with the need for
> "typename" in the macro in certain contexts). The safe_bool
> class is itself convertible to bool.

I like this general approach. First, Peter has great experience in this area, so capitalizing on his work is wise. Second, I like that most of the logic is in a class template rather than in the macro itself. That makes the macro as simple as possible and provides the opportunity to step through the code effecting the conversion should that prove desirable. Third, the use of a macro to tie the class template logic into a UDT doesn't interfere with the inheritance hierarchy and doesn't interfere with POD-ness. Lastly, I like that the expression to indicate validity is passed to the macro, which gives great flexibility and makes it explicit.

What I don't like about this approach is that the macro requires a tag argument to ensure uniqueness. This can be eliminated with a bit more code in the macro:

#define BOOST_OPERATOR_SAFE_BOOL(_expression) \
   struct _safe_bool_tag_ { }; \
   typedef ::boost::utility::safe_bool<_safe_bool_tag_> \
   operator safe_bool_type () const \
   { \
      return safe_bool_type(_expression); \

By adding a nested type and using that as the safe_bool parameterizing type, the operator invocation is simplified. Since _safe_bool_tag_ is nested, its fully qualified name is unique to the macro invocation context.

As you can see, I'm suggesting that safe_bool<T> be put in Boost.Utility. I'm also suggesting the macro be named "BOOST_OPERATOR_SAFE_BOOL."

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

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, gregod at, cpdaniel at, john at