BOOST_EXPLICIT_OPERATOR_BOOL macro?

Hi, IIUC, c+0x allows the safe bool idiom to be implemented as explicit operator bool() const { ...} but in c+03 emulating that behaviour is a bit messy. There appears to be a solution for c+03 in boost/smart_ptr/detail/operator_bool.hpp, but it's targeted for the smart_ptr classes only. Would it be possible to extract operator_bool.hpp into a reusable macro, something like: #include <boost/utility/operator_bool.hpp> struct some { BOOST_EXPLICIT_OPERATOR_BOOL( m_value != 0; ) int m_value; }; What do you think? Cheers, Christian

On 04/21/2010 07:16 PM, Christian Holmquist wrote:
Hi,
IIUC, c+0x allows the safe bool idiom to be implemented as
explicit operator bool() const { ...}
but in c+03 emulating that behaviour is a bit messy.
There appears to be a solution for c+03 in boost/smart_ptr/detail/operator_bool.hpp, but it's targeted for the smart_ptr classes only.
Would it be possible to extract operator_bool.hpp into a reusable macro, something like:
#include<boost/utility/operator_bool.hpp> struct some { BOOST_EXPLICIT_OPERATOR_BOOL( m_value != 0; )
int m_value; };
What do you think?
I think such a tool would be useful. But I found it more convenient to express explicit operator bool through operator!. This allows the check to be arbitrarily complex, and the approach adds to compatibility with some compilers. See here: <http://boost-log.svn.sourceforge.net/viewvc/boost-log/trunk/boost-log/boost/log/detail/unspecified_bool.hpp?revision=432&view=markup> Usage example: struct some { BOOST_LOG_OPERATOR_UNSPECIFIED_BOOL() bool operator! () const { return m_value == 0; } int m_value; };

Would it be possible to extract operator_bool.hpp into a reusable macro,
something like:
#include<boost/utility/operator_bool.hpp> struct some { BOOST_EXPLICIT_OPERATOR_BOOL( m_value != 0; )
int m_value; };
What do you think?
I think such a tool would be useful. But I found it more convenient to express explicit operator bool through operator!. This allows the check to be arbitrarily complex, and the approach adds to compatibility with some compilers. See here:
Usage example:
struct some { BOOST_LOG_OPERATOR_UNSPECIFIED_BOOL() bool operator! () const { return m_value == 0; }
int m_value; }; _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Nice! Maybe you could move the macro from log/detail into log/utility? There's already many small things in your utility folder IIRC, this one would make a good addition. Thanks, Christian

Andrey Semashev wrote:
I think such a tool would be useful. But I found it more convenient to express explicit operator bool through operator!. This allows the check to be arbitrarily complex, and the approach adds to compatibility with some compilers. See here:
Can you explain why the dummy struct has *two* data members? Is it because some compilers interpret a pointer-to-member-object to the first data member of a class as convertible to false? Thanks, - Jeff

On 04/23/2010 10:28 AM, Jeffrey Hellrung wrote:
Andrey Semashev wrote:
I think such a tool would be useful. But I found it more convenient to express explicit operator bool through operator!. This allows the check to be arbitrarily complex, and the approach adds to compatibility with some compilers. See here:
Can you explain why the dummy struct has *two* data members? Is it because some compilers interpret a pointer-to-member-object to the first data member of a class as convertible to false?
Yes. Apparently, some compilers use offset to the data member as the underlying representation of the member data pointer, which is zero for the first data member.
participants (3)
-
Andrey Semashev
-
Christian Holmquist
-
Jeffrey Hellrung