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

source code of boost/boost/type_traits/detail/has_postfix_operator.hpp