2 * Copyright Andrey Semashev 2007 - 2013.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
9 * \file explicit_operator_bool.hpp
10 * \author Andrey Semashev
13 * This header defines a compatibility macro that implements an unspecified
14 * \c bool operator idiom, which is superseded with explicit conversion operators in
18 #ifndef BOOST_CORE_EXPLICIT_OPERATOR_BOOL_HPP
19 #define BOOST_CORE_EXPLICIT_OPERATOR_BOOL_HPP
21 #include <boost/config.hpp>
23 #ifdef BOOST_HAS_PRAGMA_ONCE
27 #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
30 * \brief The macro defines an explicit operator of conversion to \c bool
32 * The macro should be used inside the definition of a class that has to
33 * support the conversion. The class should also implement <tt>operator!</tt>,
34 * in terms of which the conversion operator will be implemented.
36 #define BOOST_EXPLICIT_OPERATOR_BOOL()\
37 BOOST_FORCEINLINE explicit operator bool () const\
39 return !this->operator! ();\
43 * \brief The macro defines a noexcept explicit operator of conversion to \c bool
45 * The macro should be used inside the definition of a class that has to
46 * support the conversion. The class should also implement <tt>operator!</tt>,
47 * in terms of which the conversion operator will be implemented.
49 #define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\
50 BOOST_FORCEINLINE explicit operator bool () const BOOST_NOEXCEPT\
52 return !this->operator! ();\
56 * \brief The macro defines a constexpr explicit operator of conversion to \c bool
58 * The macro should be used inside the definition of a class that has to
59 * support the conversion. The class should also implement <tt>operator!</tt>,
60 * in terms of which the conversion operator will be implemented.
62 #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\
63 BOOST_FORCEINLINE BOOST_CONSTEXPR explicit operator bool () const BOOST_NOEXCEPT\
65 return !this->operator! ();\
68 #else // !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
70 #if (defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)) && !defined(BOOST_NO_COMPILER_CONFIG)
71 // Sun C++ 5.3 can't handle the safe_bool idiom, so don't use it
72 #define BOOST_NO_UNSPECIFIED_BOOL
73 #endif // (defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)) && !defined(BOOST_NO_COMPILER_CONFIG)
75 #if !defined(BOOST_NO_UNSPECIFIED_BOOL)
81 #if !defined(_MSC_VER) && !defined(__IBMCPP__)
83 struct unspecified_bool
85 // NOTE TO THE USER: If you see this in error messages then you tried
86 // to apply an unsupported operator on the object that supports
87 // explicit conversion to bool.
88 struct OPERATORS_NOT_ALLOWED;
89 static void true_value(OPERATORS_NOT_ALLOWED*) {}
91 typedef void (*unspecified_bool_type)(unspecified_bool::OPERATORS_NOT_ALLOWED*);
95 // MSVC and VACPP are too eager to convert pointer to function to void* even though they shouldn't
96 struct unspecified_bool
98 // NOTE TO THE USER: If you see this in error messages then you tried
99 // to apply an unsupported operator on the object that supports
100 // explicit conversion to bool.
101 struct OPERATORS_NOT_ALLOWED;
102 void true_value(OPERATORS_NOT_ALLOWED*) {}
104 typedef void (unspecified_bool::*unspecified_bool_type)(unspecified_bool::OPERATORS_NOT_ALLOWED*);
108 } // namespace detail
112 #define BOOST_EXPLICIT_OPERATOR_BOOL()\
113 BOOST_FORCEINLINE operator boost::detail::unspecified_bool_type () const\
115 return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\
118 #define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\
119 BOOST_FORCEINLINE operator boost::detail::unspecified_bool_type () const BOOST_NOEXCEPT\
121 return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\
124 #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\
125 BOOST_FORCEINLINE BOOST_CONSTEXPR operator boost::detail::unspecified_bool_type () const BOOST_NOEXCEPT\
127 return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\
130 #else // !defined(BOOST_NO_UNSPECIFIED_BOOL)
132 #define BOOST_EXPLICIT_OPERATOR_BOOL()\
133 BOOST_FORCEINLINE operator bool () const\
135 return !this->operator! ();\
138 #define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\
139 BOOST_FORCEINLINE operator bool () const BOOST_NOEXCEPT\
141 return !this->operator! ();\
144 #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\
145 BOOST_FORCEINLINE BOOST_CONSTEXPR operator bool () const BOOST_NOEXCEPT\
147 return !this->operator! ();\
150 #endif // !defined(BOOST_NO_UNSPECIFIED_BOOL)
152 #endif // !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
154 #endif // BOOST_CORE_EXPLICIT_OPERATOR_BOOL_HPP