]> git.donarmstrong.com Git - rsem.git/blob - boost/type_traits/detail/has_binary_operator.hpp
Updated boost to v1.55.0
[rsem.git] / boost / type_traits / detail / has_binary_operator.hpp
1 //  (C) Copyright 2009-2011 Frederic Bron, Robert Stewart, Steven Watanabe & Roman Perepelitsa.
2 //
3 //  Use, modification and distribution are subject to the Boost Software License,
4 //  Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 //  http://www.boost.org/LICENSE_1_0.txt).
6 //
7 //  See http://www.boost.org/libs/type_traits for most recent version including documentation.
8
9 #include <boost/config.hpp>
10 #include <boost/type_traits/ice.hpp>
11 #include <boost/type_traits/integral_constant.hpp>
12 #include <boost/type_traits/is_base_of.hpp>
13 #include <boost/type_traits/is_const.hpp>
14 #include <boost/type_traits/is_convertible.hpp>
15 #include <boost/type_traits/is_fundamental.hpp>
16 #include <boost/type_traits/is_integral.hpp>
17 #include <boost/type_traits/is_pointer.hpp>
18 #include <boost/type_traits/is_same.hpp>
19 #include <boost/type_traits/is_void.hpp>
20 #include <boost/type_traits/remove_cv.hpp>
21 #include <boost/type_traits/remove_pointer.hpp>
22 #include <boost/type_traits/remove_reference.hpp>
23
24 // should be the last #include
25 #include <boost/type_traits/detail/bool_trait_def.hpp>
26
27 // cannot include this header without getting warnings of the kind:
28 // gcc:
29 //    warning: value computed is not used
30 //    warning: comparison between signed and unsigned integer expressions
31 // msvc:
32 //    warning C4018: '<' : signed/unsigned mismatch
33 //    warning C4244: '+=' : conversion from 'double' to 'char', possible loss of data
34 //    warning C4547: '*' : operator before comma has no effect; expected operator with side-effect
35 //    warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
36 //    warning C4804: '<' : unsafe use of type 'bool' in operation
37 //    warning C4805: '==' : unsafe mix of type 'bool' and type 'char' in operation
38 // cannot find another implementation -> declared as system header to suppress these warnings.
39 #if defined(__GNUC__) && ((__GNUC__==3 && __GNUC_MINOR__>=1) || (__GNUC__>3))
40 #   pragma GCC system_header
41 #elif defined(BOOST_MSVC)
42 #   pragma warning ( push )
43 #   pragma warning ( disable : 4018 4244 4547 4800 4804 4805 4913 )
44 #endif
45
46 namespace boost {
47 namespace detail {
48
49 // This namespace ensures that argument-dependent name lookup does not mess things up.
50 namespace BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl) {
51
52 // 1. a function to have an instance of type T without requiring T to be default
53 // constructible
54 template <typename T> T &make();
55
56
57 // 2. we provide our operator definition for types that do not have one already
58
59 // a type returned from operator BOOST_TT_TRAIT_OP when no such operator is
60 // found in the type's own namespace (our own operator is used) so that we have
61 // a means to know that our operator was used
62 struct no_operator { };
63
64 // this class allows implicit conversions and makes the following operator
65 // definition less-preferred than any other such operators that might be found
66 // via argument-dependent name lookup
67 struct any { template <class T> any(T const&); };
68
69 // when operator BOOST_TT_TRAIT_OP is not available, this one is used
70 no_operator operator BOOST_TT_TRAIT_OP (const any&, const any&);
71
72
73 // 3. checks if the operator returns void or not
74 // conditions: Lhs!=void and Rhs!=void
75
76 // we first redefine "operator," so that we have no compilation error if
77 // operator BOOST_TT_TRAIT_OP returns void and we can use the return type of
78 // (lhs BOOST_TT_TRAIT_OP rhs, returns_void_t()) to deduce if
79 // operator BOOST_TT_TRAIT_OP returns void or not:
80 // - operator BOOST_TT_TRAIT_OP returns void   -> (lhs BOOST_TT_TRAIT_OP rhs, returns_void_t()) returns returns_void_t
81 // - operator BOOST_TT_TRAIT_OP returns !=void -> (lhs BOOST_TT_TRAIT_OP rhs, returns_void_t()) returns int
82 struct returns_void_t { };
83 template <typename T> int operator,(const T&, returns_void_t);
84 template <typename T> int operator,(const volatile T&, returns_void_t);
85
86 // this intermediate trait has member value of type bool:
87 // - value==true -> operator BOOST_TT_TRAIT_OP returns void
88 // - value==false -> operator BOOST_TT_TRAIT_OP does not return void
89 template < typename Lhs, typename Rhs >
90 struct operator_returns_void {
91    // overloads of function returns_void make the difference
92    // yes_type and no_type have different size by construction
93    static ::boost::type_traits::yes_type returns_void(returns_void_t);
94    static ::boost::type_traits::no_type returns_void(int);
95    BOOST_STATIC_CONSTANT(bool, value = (sizeof(::boost::type_traits::yes_type)==sizeof(returns_void((make<Lhs>() BOOST_TT_TRAIT_OP make<Rhs>(),returns_void_t())))));
96 };
97
98
99 // 4. checks if the return type is Ret or Ret==dont_care
100 // conditions: Lhs!=void and Rhs!=void
101
102 struct dont_care { };
103
104 template < typename Lhs, typename Rhs, typename Ret, bool Returns_void >
105 struct operator_returns_Ret;
106
107 template < typename Lhs, typename Rhs >
108 struct operator_returns_Ret < Lhs, Rhs, dont_care, true > {
109    BOOST_STATIC_CONSTANT(bool, value = true);
110 };
111
112 template < typename Lhs, typename Rhs >
113 struct operator_returns_Ret < Lhs, Rhs, dont_care, false > {
114    BOOST_STATIC_CONSTANT(bool, value = true);
115 };
116
117 template < typename Lhs, typename Rhs >
118 struct operator_returns_Ret < Lhs, Rhs, void, true > {
119    BOOST_STATIC_CONSTANT(bool, value = true);
120 };
121
122 template < typename Lhs, typename Rhs >
123 struct operator_returns_Ret < Lhs, Rhs, void, false > {
124    BOOST_STATIC_CONSTANT(bool, value = false);
125 };
126
127 template < typename Lhs, typename Rhs, typename Ret >
128 struct operator_returns_Ret < Lhs, Rhs, Ret, true > {
129    BOOST_STATIC_CONSTANT(bool, value = false);
130 };
131
132 // otherwise checks if it is convertible to Ret using the sizeof trick
133 // based on overload resolution
134 // condition: Ret!=void and Ret!=dont_care and the operator does not return void
135 template < typename Lhs, typename Rhs, typename Ret >
136 struct operator_returns_Ret < Lhs, Rhs, Ret, false > {
137    static ::boost::type_traits::yes_type is_convertible_to_Ret(Ret); // this version is preferred for types convertible to Ret
138    static ::boost::type_traits::no_type is_convertible_to_Ret(...); // this version is used otherwise
139
140    BOOST_STATIC_CONSTANT(bool, value = (sizeof(is_convertible_to_Ret(make<Lhs>() BOOST_TT_TRAIT_OP make<Rhs>()))==sizeof(::boost::type_traits::yes_type)));
141 };
142
143
144 // 5. checks for operator existence
145 // condition: Lhs!=void and Rhs!=void
146
147 // checks if our definition of operator BOOST_TT_TRAIT_OP is used or an other
148 // existing one;
149 // this is done with redefinition of "operator," that returns no_operator or has_operator
150 struct has_operator { };
151 no_operator operator,(no_operator, has_operator);
152
153 template < typename Lhs, typename Rhs >
154 struct operator_exists {
155    static ::boost::type_traits::yes_type check(has_operator); // this version is preferred when operator exists
156    static ::boost::type_traits::no_type check(no_operator); // this version is used otherwise
157
158    BOOST_STATIC_CONSTANT(bool, value = (sizeof(check(((make<Lhs>() BOOST_TT_TRAIT_OP make<Rhs>()),make<has_operator>())))==sizeof(::boost::type_traits::yes_type)));
159 };
160
161
162 // 6. main trait: to avoid any compilation error, this class behaves
163 // differently when operator BOOST_TT_TRAIT_OP(Lhs, Rhs) is forbidden by the
164 // standard.
165 // Forbidden_if is a bool that is:
166 // - true when the operator BOOST_TT_TRAIT_OP(Lhs, Rhs) is forbidden by the standard
167 //   (would yield compilation error if used)
168 // - false otherwise
169 template < typename Lhs, typename Rhs, typename Ret, bool Forbidden_if >
170 struct trait_impl1;
171
172 template < typename Lhs, typename Rhs, typename Ret >
173 struct trait_impl1 < Lhs, Rhs, Ret, true > {
174    BOOST_STATIC_CONSTANT(bool, value = false);
175 };
176
177 template < typename Lhs, typename Rhs, typename Ret >
178 struct trait_impl1 < Lhs, Rhs, Ret, false > {
179    BOOST_STATIC_CONSTANT(bool,
180       value = (
181          ::boost::type_traits::ice_and<
182             operator_exists < Lhs, Rhs >::value,
183             operator_returns_Ret < Lhs, Rhs, Ret, operator_returns_void < Lhs, Rhs >::value >::value
184          >::value
185       )
186    );
187 };
188
189 // some specializations needs to be declared for the special void case
190 template < typename Rhs, typename Ret >
191 struct trait_impl1 < void, Rhs, Ret, false > {
192    BOOST_STATIC_CONSTANT(bool, value = false);
193 };
194
195 template < typename Lhs, typename Ret >
196 struct trait_impl1 < Lhs, void, Ret, false > {
197    BOOST_STATIC_CONSTANT(bool, value = false);
198 };
199
200 template < typename Ret >
201 struct trait_impl1 < void, void, Ret, false > {
202    BOOST_STATIC_CONSTANT(bool, value = false);
203 };
204
205 // defines some typedef for convenience
206 template < typename Lhs, typename Rhs, typename Ret >
207 struct trait_impl {
208    typedef typename ::boost::remove_reference<Lhs>::type Lhs_noref;
209    typedef typename ::boost::remove_reference<Rhs>::type Rhs_noref;
210    typedef typename ::boost::remove_cv<Lhs_noref>::type Lhs_nocv;
211    typedef typename ::boost::remove_cv<Rhs_noref>::type Rhs_nocv;
212    typedef typename ::boost::remove_cv< typename ::boost::remove_reference< typename ::boost::remove_pointer<Lhs_noref>::type >::type >::type Lhs_noptr;
213    typedef typename ::boost::remove_cv< typename ::boost::remove_reference< typename ::boost::remove_pointer<Rhs_noref>::type >::type >::type Rhs_noptr;
214    BOOST_STATIC_CONSTANT(bool, value = (trait_impl1 < Lhs_noref, Rhs_noref, Ret, BOOST_TT_FORBIDDEN_IF >::value));
215 };
216
217 } // namespace impl
218 } // namespace detail
219
220 // this is the accessible definition of the trait to end user
221 BOOST_TT_AUX_BOOL_TRAIT_DEF3(BOOST_TT_TRAIT_NAME, Lhs, Rhs=Lhs, Ret=::boost::detail::BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl)::dont_care, (::boost::detail::BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl)::trait_impl < Lhs, Rhs, Ret >::value))
222
223 } // namespace boost
224
225 #if defined(BOOST_MSVC)
226 #   pragma warning ( pop )
227 #endif
228
229 #include <boost/type_traits/detail/bool_trait_undef.hpp>