1 /* 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) 6 */ 7 8 /*! 9 * \file explicit_operator_bool.hpp 10 * \author Andrey Semashev 11 * \date 08.03.2009 12 * 13 * This header defines a compatibility macro that implements an unspecified 14 * \c bool operator idiom, which is superseded with explicit conversion operators in 15 * C++11. 16 */ 17 18 #ifndef BOOST_CORE_EXPLICIT_OPERATOR_BOOL_HPP 19 #define BOOST_CORE_EXPLICIT_OPERATOR_BOOL_HPP 20 21 #include <boost/config.hpp> 22 23 #ifdef BOOST_HAS_PRAGMA_ONCE 24 #pragma once 25 #endif 26 27 #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) 28 29 /*! 30 * \brief The macro defines an explicit operator of conversion to \c bool 31 * 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. 35 */ 36 #define BOOST_EXPLICIT_OPERATOR_BOOL()\ 37 BOOST_FORCEINLINE explicit operator bool () const\ 38 {\ 39 return !this->operator! ();\ 40 } 41 42 /*! 43 * \brief The macro defines a noexcept explicit operator of conversion to \c bool 44 * 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. 48 */ 49 #define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\ 50 BOOST_FORCEINLINE explicit operator bool () const BOOST_NOEXCEPT\ 51 {\ 52 return !this->operator! ();\ 53 } 54 55 /*! 56 * \brief The macro defines a constexpr explicit operator of conversion to \c bool 57 * 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. 61 */ 62 #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\ 63 BOOST_FORCEINLINE BOOST_CONSTEXPR explicit operator bool () const BOOST_NOEXCEPT\ 64 {\ 65 return !this->operator! ();\ 66 } 67 68 #else // !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) 69 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) 74 75 #if !defined(BOOST_NO_UNSPECIFIED_BOOL) 76 77 namespace boost { 78 79 namespace detail { 80 81 #if !defined(_MSC_VER) && !defined(__IBMCPP__) 82 83 struct unspecified_bool 84 { 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; true_valueboost::detail::unspecified_bool89 static void true_value(OPERATORS_NOT_ALLOWED*) {} 90 }; 91 typedef void (*unspecified_bool_type)(unspecified_bool::OPERATORS_NOT_ALLOWED*); 92 93 #else 94 95 // MSVC and VACPP are too eager to convert pointer to function to void* even though they shouldn't 96 struct unspecified_bool 97 { 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*) {} 103 }; 104 typedef void (unspecified_bool::*unspecified_bool_type)(unspecified_bool::OPERATORS_NOT_ALLOWED*); 105 106 #endif 107 108 } // namespace detail 109 110 } // namespace boost 111 112 #define BOOST_EXPLICIT_OPERATOR_BOOL()\ 113 BOOST_FORCEINLINE operator boost::detail::unspecified_bool_type () const\ 114 {\ 115 return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\ 116 } 117 118 #define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\ 119 BOOST_FORCEINLINE operator boost::detail::unspecified_bool_type () const BOOST_NOEXCEPT\ 120 {\ 121 return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\ 122 } 123 124 #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\ 125 BOOST_FORCEINLINE BOOST_CONSTEXPR operator boost::detail::unspecified_bool_type () const BOOST_NOEXCEPT\ 126 {\ 127 return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\ 128 } 129 130 #else // !defined(BOOST_NO_UNSPECIFIED_BOOL) 131 132 #define BOOST_EXPLICIT_OPERATOR_BOOL()\ 133 BOOST_FORCEINLINE operator bool () const\ 134 {\ 135 return !this->operator! ();\ 136 } 137 138 #define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\ 139 BOOST_FORCEINLINE operator bool () const BOOST_NOEXCEPT\ 140 {\ 141 return !this->operator! ();\ 142 } 143 144 #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\ 145 BOOST_FORCEINLINE BOOST_CONSTEXPR operator bool () const BOOST_NOEXCEPT\ 146 {\ 147 return !this->operator! ();\ 148 } 149 150 #endif // !defined(BOOST_NO_UNSPECIFIED_BOOL) 151 152 #endif // !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) 153 154 #endif // BOOST_CORE_EXPLICIT_OPERATOR_BOOL_HPP 155