1 // (C) Copyright 2009-2011 Frederic Bron, Robert Stewart, Steven Watanabe & Roman Perepelitsa.
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).
7 // See http://www.boost.org/libs/type_traits for most recent version including documentation.
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_const.hpp>
13 #include <boost/type_traits/is_fundamental.hpp>
14 #include <boost/type_traits/is_integral.hpp>
15 #include <boost/type_traits/is_pointer.hpp>
16 #include <boost/type_traits/is_same.hpp>
17 #include <boost/type_traits/is_void.hpp>
18 #include <boost/type_traits/remove_cv.hpp>
19 #include <boost/type_traits/remove_pointer.hpp>
20 #include <boost/type_traits/remove_reference.hpp>
22 // should be the last #include
23 #include <boost/type_traits/detail/bool_trait_def.hpp>
25 // cannot include this header without getting warnings of the kind:
27 // warning: value computed is not used
28 // warning: comparison between signed and unsigned integer expressions
30 // warning C4146: unary minus operator applied to unsigned type, result still unsigned
31 // warning C4804: '-' : unsafe use of type 'bool' in operation
32 // cannot find another implementation -> declared as system header to suppress these warnings.
33 #if defined(__GNUC__) && ((__GNUC__==3 && __GNUC_MINOR__>=1) || (__GNUC__>3))
34 # pragma GCC system_header
35 #elif defined(BOOST_MSVC)
36 # pragma warning ( push )
37 # pragma warning ( disable : 4146 4804 4913 4244 )
43 // This namespace ensures that argument-dependent name lookup does not mess things up.
44 namespace BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl) {
46 // 1. a function to have an instance of type T without requiring T to be default
48 template <typename T> T &make();
51 // 2. we provide our operator definition for types that do not have one already
53 // a type returned from operator BOOST_TT_TRAIT_OP when no such operator is
54 // found in the type's own namespace (our own operator is used) so that we have
55 // a means to know that our operator was used
56 struct no_operator { };
58 // this class allows implicit conversions and makes the following operator
59 // definition less-preferred than any other such operators that might be found
60 // via argument-dependent name lookup
61 struct any { template <class T> any(T const&); };
63 // when operator BOOST_TT_TRAIT_OP is not available, this one is used
64 no_operator operator BOOST_TT_TRAIT_OP (const any&);
67 // 3. checks if the operator returns void or not
68 // conditions: Rhs!=void
70 // we first redefine "operator," so that we have no compilation error if
71 // operator BOOST_TT_TRAIT_OP returns void and we can use the return type of
72 // (BOOST_TT_TRAIT_OP rhs, returns_void_t()) to deduce if
73 // operator BOOST_TT_TRAIT_OP returns void or not:
74 // - operator BOOST_TT_TRAIT_OP returns void -> (BOOST_TT_TRAIT_OP rhs, returns_void_t()) returns returns_void_t
75 // - operator BOOST_TT_TRAIT_OP returns !=void -> (BOOST_TT_TRAIT_OP rhs, returns_void_t()) returns int
76 struct returns_void_t { };
77 template <typename T> int operator,(const T&, returns_void_t);
78 template <typename T> int operator,(const volatile T&, returns_void_t);
80 // this intermediate trait has member value of type bool:
81 // - value==true -> operator BOOST_TT_TRAIT_OP returns void
82 // - value==false -> operator BOOST_TT_TRAIT_OP does not return void
83 template < typename Rhs >
84 struct operator_returns_void {
85 // overloads of function returns_void make the difference
86 // yes_type and no_type have different size by construction
87 static ::boost::type_traits::yes_type returns_void(returns_void_t);
88 static ::boost::type_traits::no_type returns_void(int);
89 BOOST_STATIC_CONSTANT(bool, value = (sizeof(::boost::type_traits::yes_type)==sizeof(returns_void((BOOST_TT_TRAIT_OP make<Rhs>(),returns_void_t())))));
93 // 4. checks if the return type is Ret or Ret==dont_care
94 // conditions: Rhs!=void
98 template < typename Rhs, typename Ret, bool Returns_void >
99 struct operator_returns_Ret;
101 template < typename Rhs >
102 struct operator_returns_Ret < Rhs, dont_care, true > {
103 BOOST_STATIC_CONSTANT(bool, value = true);
106 template < typename Rhs >
107 struct operator_returns_Ret < Rhs, dont_care, false > {
108 BOOST_STATIC_CONSTANT(bool, value = true);
111 template < typename Rhs >
112 struct operator_returns_Ret < Rhs, void, true > {
113 BOOST_STATIC_CONSTANT(bool, value = true);
116 template < typename Rhs >
117 struct operator_returns_Ret < Rhs, void, false > {
118 BOOST_STATIC_CONSTANT(bool, value = false);
121 template < typename Rhs, typename Ret >
122 struct operator_returns_Ret < Rhs, Ret, true > {
123 BOOST_STATIC_CONSTANT(bool, value = false);
126 // otherwise checks if it is convertible to Ret using the sizeof trick
127 // based on overload resolution
128 // condition: Ret!=void and Ret!=dont_care and the operator does not return void
129 template < typename Rhs, typename Ret >
130 struct operator_returns_Ret < Rhs, Ret, false > {
131 static ::boost::type_traits::yes_type is_convertible_to_Ret(Ret); // this version is preferred for types convertible to Ret
132 static ::boost::type_traits::no_type is_convertible_to_Ret(...); // this version is used otherwise
134 BOOST_STATIC_CONSTANT(bool, value = (sizeof(is_convertible_to_Ret(BOOST_TT_TRAIT_OP make<Rhs>()))==sizeof(::boost::type_traits::yes_type)));
138 // 5. checks for operator existence
139 // condition: Rhs!=void
141 // checks if our definition of operator BOOST_TT_TRAIT_OP is used or an other
143 // this is done with redefinition of "operator," that returns no_operator or has_operator
144 struct has_operator { };
145 no_operator operator,(no_operator, has_operator);
147 template < typename Rhs >
148 struct operator_exists {
149 static ::boost::type_traits::yes_type check(has_operator); // this version is preferred when operator exists
150 static ::boost::type_traits::no_type check(no_operator); // this version is used otherwise
152 BOOST_STATIC_CONSTANT(bool, value = (sizeof(check(((BOOST_TT_TRAIT_OP make<Rhs>()),make<has_operator>())))==sizeof(::boost::type_traits::yes_type)));
156 // 6. main trait: to avoid any compilation error, this class behaves
157 // differently when operator BOOST_TT_TRAIT_OP(Rhs) is forbidden by the
159 // Forbidden_if is a bool that is:
160 // - true when the operator BOOST_TT_TRAIT_OP(Rhs) is forbidden by the standard
161 // (would yield compilation error if used)
163 template < typename Rhs, typename Ret, bool Forbidden_if >
166 template < typename Rhs, typename Ret >
167 struct trait_impl1 < Rhs, Ret, true > {
168 BOOST_STATIC_CONSTANT(bool, value = false);
171 template < typename Rhs, typename Ret >
172 struct trait_impl1 < Rhs, Ret, false > {
173 BOOST_STATIC_CONSTANT(bool,
175 ::boost::type_traits::ice_and<
176 operator_exists < Rhs >::value,
177 operator_returns_Ret < Rhs, Ret, operator_returns_void < Rhs >::value >::value
183 // specialization needs to be declared for the special void case
184 template < typename Ret >
185 struct trait_impl1 < void, Ret, false > {
186 BOOST_STATIC_CONSTANT(bool, value = false);
189 // defines some typedef for convenience
190 template < typename Rhs, typename Ret >
192 typedef typename ::boost::remove_reference<Rhs>::type Rhs_noref;
193 typedef typename ::boost::remove_cv<Rhs_noref>::type Rhs_nocv;
194 typedef typename ::boost::remove_cv< typename ::boost::remove_reference< typename ::boost::remove_pointer<Rhs_noref>::type >::type >::type Rhs_noptr;
195 BOOST_STATIC_CONSTANT(bool, value = (trait_impl1 < Rhs_noref, Ret, BOOST_TT_FORBIDDEN_IF >::value));
199 } // namespace detail
201 // this is the accessible definition of the trait to end user
202 BOOST_TT_AUX_BOOL_TRAIT_DEF2(BOOST_TT_TRAIT_NAME, Rhs, Ret=::boost::detail::BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl)::dont_care, (::boost::detail::BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl)::trait_impl < Rhs, Ret >::value))
206 #if defined(BOOST_MSVC)
207 # pragma warning ( pop )
210 #include <boost/type_traits/detail/bool_trait_undef.hpp>