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_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>
21
22// cannot include this header without getting warnings of the kind:
23// gcc:
24// warning: value computed is not used
25// warning: comparison between signed and unsigned integer expressions
26// msvc:
27// warning C4146: unary minus operator applied to unsigned type, result still unsigned
28// warning C4804: '-' : unsafe use of type 'bool' in operation
29// cannot find another implementation -> declared as system header to suppress these warnings.
30#if defined(__GNUC__)
31# pragma GCC system_header
32#elif defined(BOOST_MSVC)
33# pragma warning ( push )
34# pragma warning ( disable : 4146 4804 4913 4244)
35# if BOOST_WORKAROUND(BOOST_MSVC_FULL_VER, >= 140050000)
36# pragma warning ( disable : 6334)
37# endif
38#endif
39
40
41
42namespace boost {
43namespace detail {
44
45// This namespace ensures that argument-dependent name lookup does not mess things up.
46namespace BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl) {
47
48// 1. a function to have an instance of type T without requiring T to be default
49// constructible
50template <typename T> T &make();
51
52
53// 2. we provide our operator definition for types that do not have one already
54
55// a type returned from operator BOOST_TT_TRAIT_OP when no such operator is
56// found in the type's own namespace (our own operator is used) so that we have
57// a means to know that our operator was used
58struct no_operator { };
59
60// this class allows implicit conversions and makes the following operator
61// definition less-preferred than any other such operators that might be found
62// via argument-dependent name lookup
63struct any { template <class T> any(T const&); };
64
65// when operator BOOST_TT_TRAIT_OP is not available, this one is used
66no_operator operator BOOST_TT_TRAIT_OP (const any&);
67
68
69// 3. checks if the operator returns void or not
70// conditions: Rhs!=void
71
72// we first redefine "operator," so that we have no compilation error if
73// operator BOOST_TT_TRAIT_OP returns void and we can use the return type of
74// (BOOST_TT_TRAIT_OP rhs, returns_void_t()) to deduce if
75// operator BOOST_TT_TRAIT_OP returns void or not:
76// - operator BOOST_TT_TRAIT_OP returns void -> (BOOST_TT_TRAIT_OP rhs, returns_void_t()) returns returns_void_t
77// - operator BOOST_TT_TRAIT_OP returns !=void -> (BOOST_TT_TRAIT_OP rhs, returns_void_t()) returns int
78struct returns_void_t { };
79template <typename T> int operator,(const T&, returns_void_t);
80template <typename T> int operator,(const volatile T&, returns_void_t);
81
82// this intermediate trait has member value of type bool:
83// - value==true -> operator BOOST_TT_TRAIT_OP returns void
84// - value==false -> operator BOOST_TT_TRAIT_OP does not return void
85template < typename Rhs >
86struct operator_returns_void {
87 // overloads of function returns_void make the difference
88 // yes_type and no_type have different size by construction
89 static ::boost::type_traits::yes_type returns_void(returns_void_t);
90 static ::boost::type_traits::no_type returns_void(int);
91 BOOST_STATIC_CONSTANT(bool, value = (sizeof(::boost::type_traits::yes_type)==sizeof(returns_void((BOOST_TT_TRAIT_OP make<Rhs>(),returns_void_t())))));
92};
93
94
95// 4. checks if the return type is Ret or Ret==dont_care
96// conditions: Rhs!=void
97
98struct dont_care { };
99
100template < typename Rhs, typename Ret, bool Returns_void >
101struct operator_returns_Ret;
102
103template < typename Rhs >
104struct operator_returns_Ret < Rhs, dont_care, true > {
105 BOOST_STATIC_CONSTANT(bool, value = true);
106};
107
108template < typename Rhs >
109struct operator_returns_Ret < Rhs, dont_care, false > {
110 BOOST_STATIC_CONSTANT(bool, value = true);
111};
112
113template < typename Rhs >
114struct operator_returns_Ret < Rhs, void, true > {
115 BOOST_STATIC_CONSTANT(bool, value = true);
116};
117
118template < typename Rhs >
119struct operator_returns_Ret < Rhs, void, false > {
120 BOOST_STATIC_CONSTANT(bool, value = false);
121};
122
123template < typename Rhs, typename Ret >
124struct operator_returns_Ret < Rhs, Ret, true > {
125 BOOST_STATIC_CONSTANT(bool, value = false);
126};
127
128// otherwise checks if it is convertible to Ret using the sizeof trick
129// based on overload resolution
130// condition: Ret!=void and Ret!=dont_care and the operator does not return void
131template < typename Rhs, typename Ret >
132struct operator_returns_Ret < Rhs, Ret, false > {
133 static ::boost::type_traits::yes_type is_convertible_to_Ret(Ret); // this version is preferred for types convertible to Ret
134 static ::boost::type_traits::no_type is_convertible_to_Ret(...); // this version is used otherwise
135
136 BOOST_STATIC_CONSTANT(bool, value = (sizeof(is_convertible_to_Ret(BOOST_TT_TRAIT_OP make<Rhs>()))==sizeof(::boost::type_traits::yes_type)));
137};
138
139
140// 5. checks for operator existence
141// condition: Rhs!=void
142
143// checks if our definition of operator BOOST_TT_TRAIT_OP is used or an other
144// existing one;
145// this is done with redefinition of "operator," that returns no_operator or has_operator
146struct has_operator { };
147no_operator operator,(no_operator, has_operator);
148
149template < typename Rhs >
150struct operator_exists {
151 static ::boost::type_traits::yes_type s_check(has_operator); // this version is preferred when operator exists
152 static ::boost::type_traits::no_type s_check(no_operator); // this version is used otherwise
153
154 BOOST_STATIC_CONSTANT(bool, value = (sizeof(s_check(((BOOST_TT_TRAIT_OP make<Rhs>()),make<has_operator>())))==sizeof(::boost::type_traits::yes_type)));
155};
156
157
158// 6. main trait: to avoid any compilation error, this class behaves
159// differently when operator BOOST_TT_TRAIT_OP(Rhs) is forbidden by the
160// standard.
161// Forbidden_if is a bool that is:
162// - true when the operator BOOST_TT_TRAIT_OP(Rhs) is forbidden by the standard
163// (would yield compilation error if used)
164// - false otherwise
165template < typename Rhs, typename Ret, bool Forbidden_if >
166struct trait_impl1;
167
168template < typename Rhs, typename Ret >
169struct trait_impl1 < Rhs, Ret, true > {
170 BOOST_STATIC_CONSTANT(bool, value = false);
171};
172
173template < typename Rhs, typename Ret >
174struct trait_impl1 < Rhs, Ret, false > {
175 BOOST_STATIC_CONSTANT(bool,
176 value = (operator_exists < Rhs >::value && operator_returns_Ret < Rhs, Ret, operator_returns_void < Rhs >::value >::value));
177};
178
179// specialization needs to be declared for the special void case
180template < typename Ret >
181struct trait_impl1 < void, Ret, false > {
182 BOOST_STATIC_CONSTANT(bool, value = false);
183};
184
185// defines some typedef for convenience
186template < typename Rhs, typename Ret >
187struct trait_impl {
188 typedef typename ::boost::remove_reference<Rhs>::type Rhs_noref;
189 typedef typename ::boost::remove_cv<Rhs_noref>::type Rhs_nocv;
190 typedef typename ::boost::remove_cv< typename ::boost::remove_reference< typename ::boost::remove_pointer<Rhs_noref>::type >::type >::type Rhs_noptr;
191 BOOST_STATIC_CONSTANT(bool, value = (trait_impl1 < Rhs_noref, Ret, BOOST_TT_FORBIDDEN_IF >::value));
192};
193
194} // namespace impl
195} // namespace detail
196
197// this is the accessible definition of the trait to end user
198template <class Rhs, class Ret=::boost::detail::BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl)::dont_care>
199struct BOOST_TT_TRAIT_NAME : public integral_constant<bool, (::boost::detail::BOOST_JOIN(BOOST_TT_TRAIT_NAME, _impl)::trait_impl < Rhs, Ret >::value)>{};
200
201} // namespace boost
202
203#if defined(BOOST_MSVC)
204# pragma warning ( pop )
205#endif
206