Boost logo

Boost :

Subject: Re: [boost] safe-bool CRTP class
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2011-05-28 15:23:48


On Sat, May 28, 2011 at 6:22 AM, Krzysztof Czainski <1czajnik_at_[hidden]>wrote:

> Thank you all for such large input ;-)
>
> So the way I see it, most of us agree it would be nice to see one official
> safe-bool implementation tool in boost.
>
> The first question is: should it be a CRTP class, or should it be a macro,
> or both perhaps?
>
[...]

> Macro usage:
> BOOST_EXPLICIT_BOOL_OPERATOR() const {
> return ...;
> }
>
> I'm not sure how the macro would be implemented, but we'll get to that once
> we make the choice.
>
> Right now I feel convinced to the macro version.
>

Here's a trial attempt at an implementation of the macro (warning: not
tested):

#ifndef BOOST_NO_EXPLICIT_CONVERSION_OPERATORS

#define BOOST_EXPLICIT_OPERATOR_BOOL() \
explicit operator bool()

#else // #ifndef BOOST_NO_EXPLICIT_CONVERSION_OPERATORS

#ifndef BOOST_NO_EXPLICIT_OPERATOR_BOOL

#define BOOST_EXPLICIT_OPERATOR_BOOL() \
struct _boost_explicit_operator_bool_struct { int _dummy; int member; }; \
typedef int
(_boost_explicit_operator_bool_struct::*_boost_explicit_operator_bool_type);
\
operator _boost_explicit_operator_bool_type() const \
{ return _boost_explicit_operator_bool_impl() ?
&_boost_explicit_operator_bool_struct::member : 0; } \
bool _boost_explicit_operator_bool_impl()

#else // #ifndef BOOST_NO_EXPLICIT_OPERATOR_BOOL

#define BOOST_EXPLICIT_OPERATOR_BOOL() \
operator bool()

#endif // #ifndef BOOST_NO_EXPLICIT_OPERATOR_BOOL

#endif // #ifndef BOOST_NO_EXPLICIT_CONVERSION_OPERATORS

...and we can, optionally, give _boost_explicit_operator_bool_struct and
_boost_explicit_operator_bool_type private access, but I don't see a way to
give _boost_explicit_operator_bool_impl private access, so the point might
be moot.

- Jeff


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