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/detail/yes_no_type.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// cannot include this header without getting warnings of the kind:
25// gcc:
26// warning: value computed is not used
27// warning: comparison between signed and unsigned integer expressions
28// msvc:
29// warning C4018: '<' : signed/unsigned mismatch
30// warning C4244: '+=' : conversion from 'double' to 'char', possible loss of data
31// warning C4547: '*' : operator before comma has no effect; expected operator with side-effect
32// warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
33// warning C4804: '<' : unsafe use of type 'bool' in operation
34// warning C4805: '==' : unsafe mix of type 'bool' and type 'char' in operation
35// cannot find another implementation -> declared as system header to suppress these warnings.
36#if defined(__GNUC__)
37# pragma GCC system_header
38#elif defined(BOOST_MSVC)
39# pragma warning ( push )
40# pragma warning ( disable : 4018 4244 4547 4800 4804 4805 4913)
41# if BOOST_WORKAROUND(BOOST_MSVC_FULL_VER, >= 140050000)
42# pragma warning ( disable : 6334)
43# endif
44#endif
45
46namespace boost {
47namespace detail {
48
49// This namespace ensures that argument-dependent name lookup does not mess things up.
50namespace 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
54template <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
62struct 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
67struct any { template <class T> any(T const&); };
68
69// when operator BOOST_TT_TRAIT_OP is not available, this one is used
70no_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
82struct returns_void_t { };
83template <typename T> int operator,(const T&, returns_void_t);
84template <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
89template < typename Lhs, typename Rhs >
90struct 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
102struct dont_care { };
103
104template < typename Lhs, typename Rhs, typename Ret, bool Returns_void >
105struct operator_returns_Ret;
106
107template < typename Lhs, typename Rhs >
108struct operator_returns_Ret < Lhs, Rhs, dont_care, true > {
109 BOOST_STATIC_CONSTANT(bool, value = true);
110};
111
112template < typename Lhs, typename Rhs >
113struct operator_returns_Ret < Lhs, Rhs, dont_care, false > {
114 BOOST_STATIC_CONSTANT(bool, value = true);
115};
116
117template < typename Lhs, typename Rhs >
118struct operator_returns_Ret < Lhs, Rhs, void, true > {
119 BOOST_STATIC_CONSTANT(bool, value = true);
120};
121
122template < typename Lhs, typename Rhs >
123struct operator_returns_Ret < Lhs, Rhs, void, false > {
124 BOOST_STATIC_CONSTANT(bool, value = false);
125};
126
127template < typename Lhs, typename Rhs, typename Ret >
128struct 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
135template < typename Lhs, typename Rhs, typename Ret >
136struct 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
150struct has_operator { };
151no_operator operator,(no_operator, has_operator);
152
153template < typename Lhs, typename Rhs >
154struct operator_exists {
155 static ::boost::type_traits::yes_type s_check(has_operator); // this version is preferred when operator exists
156 static ::boost::type_traits::no_type s_check(no_operator); // this version is used otherwise
157
158 BOOST_STATIC_CONSTANT(bool, value = (sizeof(s_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
169template < typename Lhs, typename Rhs, typename Ret, bool Forbidden_if >
170struct trait_impl1;
171
172template < typename Lhs, typename Rhs, typename Ret >
173struct trait_impl1 < Lhs, Rhs, Ret, true > {
174 BOOST_STATIC_CONSTANT(bool, value = false);
175};
176
177template < typename Lhs, typename Rhs, typename Ret >
178struct trait_impl1 < Lhs, Rhs, Ret, false > {
179 BOOST_STATIC_CONSTANT(bool,
180 value = (operator_exists < Lhs, Rhs >::value && operator_returns_Ret < Lhs, Rhs, Ret, operator_returns_void < Lhs, Rhs >::value >::value));
181};
182
183// some specializations needs to be declared for the special void case
184template < typename Rhs, typename Ret >
185struct trait_impl1 < void, Rhs, Ret, false > {
186 BOOST_STATIC_CONSTANT(bool, value = false);
187};
188
189template < typename Lhs, typename Ret >
190struct trait_impl1 < Lhs, void, Ret, false > {
191 BOOST_STATIC_CONSTANT(bool, value = false);
192};
193
194template < typename Ret >
195struct trait_impl1 < void, void, Ret, false > {
196 BOOST_STATIC_CONSTANT(bool, value = false);
197};
198
199// defines some typedef for convenience
200template < typename Lhs, typename Rhs, typename Ret >
201struct trait_impl {
202 typedef typename ::boost::remove_reference<Lhs>::type Lhs_noref;
203 typedef typename ::boost::remove_reference<Rhs>::type Rhs_noref;
204 typedef typename ::boost::remove_cv<Lhs_noref>::type Lhs_nocv;
205 typedef typename ::boost::remove_cv<Rhs_noref>::type Rhs_nocv;
206 typedef typename ::boost::remove_cv< typename ::boost::remove_reference< typename ::boost::remove_pointer<Lhs_noref>::type >::type >::type Lhs_noptr;
207 typedef typename ::boost::remove_cv< typename ::boost::remove_reference< typename ::boost::remove_pointer<Rhs_noref>::type >::type >::type Rhs_noptr;
208 BOOST_STATIC_CONSTANT(bool, value = (trait_impl1 < Lhs_noref, Rhs_noref, Ret, BOOST_TT_FORBIDDEN_IF >::value));
209};
210
211} // namespace impl
212} // namespace detail
213
214// this is the accessible definition of the trait to end user
215template <class Lhs, class Rhs=Lhs, class Ret=::boost::detail::BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl)::dont_care>
216struct BOOST_TT_TRAIT_NAME : public integral_constant<bool, (::boost::detail::BOOST_JOIN(BOOST_TT_TRAIT_NAME, _impl)::trait_impl < Lhs, Rhs, Ret >::value)>{};
217
218} // namespace boost
219
220#if defined(BOOST_MSVC)
221# pragma warning ( pop )
222#endif
223