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>
22 #include <boost/config/workaround.hpp>
24 #ifdef BOOST_HAS_PRAGMA_ONCE
28 #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
31 * \brief The macro defines an explicit operator of conversion to \c bool
33 * The macro should be used inside the definition of a class that has to
34 * support the conversion. The class should also implement <tt>operator!</tt>,
35 * in terms of which the conversion operator will be implemented.
37 #define BOOST_EXPLICIT_OPERATOR_BOOL()\
38 BOOST_FORCEINLINE explicit operator bool () const\
40 return !this->operator! ();\
44 * \brief The macro defines a noexcept explicit operator of conversion to \c bool
46 * The macro should be used inside the definition of a class that has to
47 * support the conversion. The class should also implement <tt>operator!</tt>,
48 * in terms of which the conversion operator will be implemented.
50 #define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\
51 BOOST_FORCEINLINE explicit operator bool () const BOOST_NOEXCEPT\
53 return !this->operator! ();\
56 #if !BOOST_WORKAROUND(BOOST_GCC, < 40700)
59 * \brief The macro defines a constexpr explicit operator of conversion to \c bool
61 * The macro should be used inside the definition of a class that has to
62 * support the conversion. The class should also implement <tt>operator!</tt>,
63 * in terms of which the conversion operator will be implemented.
65 #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\
66 BOOST_FORCEINLINE BOOST_CONSTEXPR explicit operator bool () const BOOST_NOEXCEPT\
68 return !this->operator! ();\
73 #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL() BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
77 #else // !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
79 #if (defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)) && !defined(BOOST_NO_COMPILER_CONFIG)
80 // Sun C++ 5.3 can't handle the safe_bool idiom, so don't use it
81 #define BOOST_NO_UNSPECIFIED_BOOL
82 #endif // (defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)) && !defined(BOOST_NO_COMPILER_CONFIG)
84 #if !defined(BOOST_NO_UNSPECIFIED_BOOL)
90 #if !defined(_MSC_VER) && !defined(__IBMCPP__)
92 struct unspecified_bool
94 // NOTE TO THE USER: If you see this in error messages then you tried
95 // to apply an unsupported operator on the object that supports
96 // explicit conversion to bool.
97 struct OPERATORS_NOT_ALLOWED;
98 static void true_value(OPERATORS_NOT_ALLOWED*) {}
100 typedef void (*unspecified_bool_type)(unspecified_bool::OPERATORS_NOT_ALLOWED*);
104 // MSVC and VACPP are too eager to convert pointer to function to void* even though they shouldn't
105 struct unspecified_bool
107 // NOTE TO THE USER: If you see this in error messages then you tried
108 // to apply an unsupported operator on the object that supports
109 // explicit conversion to bool.
110 struct OPERATORS_NOT_ALLOWED;
111 void true_value(OPERATORS_NOT_ALLOWED*) {}
113 typedef void (unspecified_bool::*unspecified_bool_type)(unspecified_bool::OPERATORS_NOT_ALLOWED*);
117 } // namespace detail
121 #define BOOST_EXPLICIT_OPERATOR_BOOL()\
122 BOOST_FORCEINLINE operator boost::detail::unspecified_bool_type () const\
124 return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\
127 #define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\
128 BOOST_FORCEINLINE operator boost::detail::unspecified_bool_type () const BOOST_NOEXCEPT\
130 return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\
133 #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\
134 BOOST_FORCEINLINE BOOST_CONSTEXPR operator boost::detail::unspecified_bool_type () const BOOST_NOEXCEPT\
136 return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\
139 #else // !defined(BOOST_NO_UNSPECIFIED_BOOL)
141 #define BOOST_EXPLICIT_OPERATOR_BOOL()\
142 BOOST_FORCEINLINE operator bool () const\
144 return !this->operator! ();\
147 #define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\
148 BOOST_FORCEINLINE operator bool () const BOOST_NOEXCEPT\
150 return !this->operator! ();\
153 #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\
154 BOOST_FORCEINLINE BOOST_CONSTEXPR operator bool () const BOOST_NOEXCEPT\
156 return !this->operator! ();\
159 #endif // !defined(BOOST_NO_UNSPECIFIED_BOOL)
161 #endif // !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
163 #endif // BOOST_CORE_EXPLICIT_OPERATOR_BOOL_HPP