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 | #include <boost/config/workaround.hpp> |
23 | |
24 | #ifdef BOOST_HAS_PRAGMA_ONCE |
25 | #pragma once |
26 | #endif |
27 | |
28 | #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) |
29 | |
30 | /*! |
31 | * \brief The macro defines an explicit operator of conversion to \c bool |
32 | * |
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. |
36 | */ |
37 | #define BOOST_EXPLICIT_OPERATOR_BOOL()\ |
38 | BOOST_FORCEINLINE explicit operator bool () const\ |
39 | {\ |
40 | return !this->operator! ();\ |
41 | } |
42 | |
43 | /*! |
44 | * \brief The macro defines a noexcept explicit operator of conversion to \c bool |
45 | * |
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. |
49 | */ |
50 | #define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\ |
51 | BOOST_FORCEINLINE explicit operator bool () const BOOST_NOEXCEPT\ |
52 | {\ |
53 | return !this->operator! ();\ |
54 | } |
55 | |
56 | #if !BOOST_WORKAROUND(BOOST_GCC, < 40700) |
57 | |
58 | /*! |
59 | * \brief The macro defines a constexpr explicit operator of conversion to \c bool |
60 | * |
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. |
64 | */ |
65 | #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\ |
66 | BOOST_FORCEINLINE BOOST_CONSTEXPR explicit operator bool () const BOOST_NOEXCEPT\ |
67 | {\ |
68 | return !this->operator! ();\ |
69 | } |
70 | |
71 | #else |
72 | |
73 | #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL() BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT() |
74 | |
75 | #endif |
76 | |
77 | #else // !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) |
78 | |
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) |
83 | |
84 | #if !defined(BOOST_NO_UNSPECIFIED_BOOL) |
85 | |
86 | namespace boost { |
87 | |
88 | namespace detail { |
89 | |
90 | #if !defined(_MSC_VER) && !defined(__IBMCPP__) |
91 | |
92 | struct unspecified_bool |
93 | { |
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*) {} |
99 | }; |
100 | typedef void (*unspecified_bool_type)(unspecified_bool::OPERATORS_NOT_ALLOWED*); |
101 | |
102 | #else |
103 | |
104 | // MSVC and VACPP are too eager to convert pointer to function to void* even though they shouldn't |
105 | struct unspecified_bool |
106 | { |
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*) {} |
112 | }; |
113 | typedef void (unspecified_bool::*unspecified_bool_type)(unspecified_bool::OPERATORS_NOT_ALLOWED*); |
114 | |
115 | #endif |
116 | |
117 | } // namespace detail |
118 | |
119 | } // namespace boost |
120 | |
121 | #define BOOST_EXPLICIT_OPERATOR_BOOL()\ |
122 | BOOST_FORCEINLINE operator boost::detail::unspecified_bool_type () const\ |
123 | {\ |
124 | return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\ |
125 | } |
126 | |
127 | #define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\ |
128 | BOOST_FORCEINLINE operator boost::detail::unspecified_bool_type () const BOOST_NOEXCEPT\ |
129 | {\ |
130 | return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\ |
131 | } |
132 | |
133 | #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\ |
134 | BOOST_FORCEINLINE BOOST_CONSTEXPR operator boost::detail::unspecified_bool_type () const BOOST_NOEXCEPT\ |
135 | {\ |
136 | return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\ |
137 | } |
138 | |
139 | #else // !defined(BOOST_NO_UNSPECIFIED_BOOL) |
140 | |
141 | #define BOOST_EXPLICIT_OPERATOR_BOOL()\ |
142 | BOOST_FORCEINLINE operator bool () const\ |
143 | {\ |
144 | return !this->operator! ();\ |
145 | } |
146 | |
147 | #define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\ |
148 | BOOST_FORCEINLINE operator bool () const BOOST_NOEXCEPT\ |
149 | {\ |
150 | return !this->operator! ();\ |
151 | } |
152 | |
153 | #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\ |
154 | BOOST_FORCEINLINE BOOST_CONSTEXPR operator bool () const BOOST_NOEXCEPT\ |
155 | {\ |
156 | return !this->operator! ();\ |
157 | } |
158 | |
159 | #endif // !defined(BOOST_NO_UNSPECIFIED_BOOL) |
160 | |
161 | #endif // !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) |
162 | |
163 | #endif // BOOST_CORE_EXPLICIT_OPERATOR_BOOL_HPP |
164 | |