llvm.org GIT mirror llvm / 1dd3111
[C++11] Now that the users are gone, rip out the duplicated traits from type_traits.h Simplify the remaining ones a bit. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203249 91177308-0d34-0410-b5e6-96231b3b80d8 Benjamin Kramer 5 years ago
2 changed file(s) with 28 addition(s) and 179 deletion(s). Raw diff Collapse all Expand all
66 //
77 //===----------------------------------------------------------------------===//
88 //
9 // This file provides a template class that determines if a type is a class or
10 // not. The basic mechanism, based on using the pointer to member function of
11 // a zero argument to a function was "boosted" from the boost type_traits
12 // library. See http://www.boost.org/ for all the gory details.
9 // This file provides useful additions to the standard type_traits library.
1310 //
1411 //===----------------------------------------------------------------------===//
1512
1613 #ifndef LLVM_SUPPORT_TYPE_TRAITS_H
1714 #define LLVM_SUPPORT_TYPE_TRAITS_H
1815
19 #include "llvm/Support/DataTypes.h"
20 #include <cstddef>
16 #include <type_traits>
2117 #include
2218
2319 #ifndef __has_feature
2521 #define __has_feature(x) 0
2622 #endif
2723
28 // This is actually the conforming implementation which works with abstract
29 // classes. However, enough compilers have trouble with it that most will use
30 // the one in boost/type_traits/object_traits.hpp. This implementation actually
31 // works with VC7.0, but other interactions seem to fail when we use it.
24 namespace llvm {
3225
33 namespace llvm {
34
35 namespace dont_use
36 {
37 // These two functions should never be used. They are helpers to
38 // the is_class template below. They cannot be located inside
39 // is_class because doing so causes at least GCC to think that
40 // the value of the "value" enumerator is not constant. Placing
41 // them out here (for some strange reason) allows the sizeof
42 // operator against them to magically be constant. This is
43 // important to make the is_class::value idiom zero cost. it
44 // evaluates to a constant 1 or 0 depending on whether the
45 // parameter T is a class or not (respectively).
46 template char is_class_helper(void(T::*)());
47 template double is_class_helper(...);
48 }
49
50 template
51 struct is_class
52 {
53 // is_class<> metafunction due to Paul Mensonides (leavings@attbi.com). For
54 // more details:
55 // http://groups.google.com/groups?hl=en&selm=000001c1cc83%24e154d5e0%247772e50c%40c161550a&rnum=1
56 public:
57 static const bool value =
58 sizeof(char) == sizeof(dont_use::is_class_helper(0));
59 };
60
61
6226 /// isPodLike - This is a type trait that is used to determine whether a given
6327 /// type can be copied around with memcpy instead of running ctors etc.
6428 template
7034 #else
7135 // If we don't know anything else, we can (at least) assume that all non-class
7236 // types are PODs.
73 static const bool value = !is_class::value;
37 static const bool value = !std::is_class::value;
7438 #endif
7539 };
7640
7943 struct isPodLike > {
8044 static const bool value = isPodLike::value && isPodLike::value;
8145 };
82
83
84 template
85 struct integral_constant {
86 typedef T value_type;
87 static const value_type value = v;
88 typedef integral_constant type;
89 operator value_type() { return value; }
90 };
91
92 typedef integral_constant true_type;
93 typedef integral_constant false_type;
94
95 /// \brief Metafunction that determines whether the two given types are
96 /// equivalent.
97 template struct is_same : public false_type {};
98 template struct is_same : public true_type {};
99
100 /// \brief Metafunction that removes const qualification from a type.
101 template struct remove_const { typedef T type; };
102 template struct remove_const { typedef T type; };
103
104 /// \brief Metafunction that removes volatile qualification from a type.
105 template struct remove_volatile { typedef T type; };
106 template struct remove_volatile { typedef T type; };
107
108 /// \brief Metafunction that removes both const and volatile qualification from
109 /// a type.
110 template struct remove_cv {
111 typedef typename remove_const::type>::type type;
112 };
113
114 /// \brief Helper to implement is_integral metafunction.
115 template struct is_integral_impl : false_type {};
116 template <> struct is_integral_impl< bool> : true_type {};
117 template <> struct is_integral_impl< char> : true_type {};
118 template <> struct is_integral_impl< signed char> : true_type {};
119 template <> struct is_integral_impl : true_type {};
120 template <> struct is_integral_impl< wchar_t> : true_type {};
121 template <> struct is_integral_impl< short> : true_type {};
122 template <> struct is_integral_impl : true_type {};
123 template <> struct is_integral_impl< int> : true_type {};
124 template <> struct is_integral_impl : true_type {};
125 template <> struct is_integral_impl< long> : true_type {};
126 template <> struct is_integral_impl : true_type {};
127 template <> struct is_integral_impl< long long> : true_type {};
128 template <> struct is_integral_impl : true_type {};
129
130 /// \brief Metafunction that determines whether the given type is an integral
131 /// type.
132 template
133 struct is_integral : is_integral_impl {};
134
135 /// \brief Metafunction to remove reference from a type.
136 template struct remove_reference { typedef T type; };
137 template struct remove_reference { typedef T type; };
138
139 /// \brief Metafunction that determines whether the given type is a pointer
140 /// type.
141 template struct is_pointer : false_type {};
142 template struct is_pointer : true_type {};
143 template struct is_pointer : true_type {};
144 template struct is_pointer : true_type {};
145 template struct is_pointer : true_type {};
146
147 /// \brief Metafunction that determines wheather the given type is a reference.
148 template struct is_reference : false_type {};
149 template struct is_reference : true_type {};
15046
15147 /// \brief Metafunction that determines whether the given type is either an
15248 /// integral type or an enumeration type.
15349 ///
154 /// Note that this accepts potentially more integral types than we whitelist
155 /// above for is_integral because it is based on merely being convertible
156 /// implicitly to an integral type.
50 /// Note that this accepts potentially more integral types than is_integral
51 /// because it is based on merely being convertible implicitly to an integral
52 /// type.
15753 template class is_integral_or_enum {
158 // Provide an overload which can be called with anything implicitly
159 // convertible to an unsigned long long. This should catch integer types and
160 // enumeration types at least. We blacklist classes with conversion operators
161 // below.
162 static double check_int_convertible(unsigned long long);
163 static char check_int_convertible(...);
164
165 typedef typename remove_reference::type UnderlyingT;
166 static UnderlyingT &nonce_instance;
54 typedef typename std::remove_reference::type UnderlyingT;
16755
16856 public:
169 static const bool
170 value = (!is_class::value && !is_pointer::value &&
171 !is_same::value &&
172 !is_same::value &&
173 sizeof(char) != sizeof(check_int_convertible(nonce_instance)));
57 static const bool value =
58 !std::is_class::value && // Filter conversion operators.
59 !std::is_pointer::value &&
60 !std::is_floating_point::value &&
61 std::is_convertible::value;
17462 };
17563
176 // enable_if_c - Enable/disable a template based on a metafunction
177 template
178 struct enable_if_c {
64 /// \brief If T is a pointer, just return it. If it is not, return T&.
65 template
66 struct add_lvalue_reference_if_not_pointer { typedef T &type; };
67
68 template
69 struct add_lvalue_reference_if_not_pointer<
70 T, typename std::enable_if::value>::type> {
17971 typedef T type;
18072 };
18173
182 template struct enable_if_c { };
183
184 // enable_if - Enable/disable a template based on a metafunction
185 template
186 struct enable_if : public enable_if_c { };
187
188 namespace dont_use {
189 template char base_of_helper(const volatile Base*);
190 template double base_of_helper(...);
191 }
192
193 /// is_base_of - Metafunction to determine whether one type is a base class of
194 /// (or identical to) another type.
195 template
196 struct is_base_of {
197 static const bool value
198 = is_class::value && is_class::value &&
199 sizeof(char) == sizeof(dont_use::base_of_helper((Derived*)0));
200 };
201
202 // remove_pointer - Metafunction to turn Foo* into Foo. Defined in
203 // C++0x [meta.trans.ptr].
204 template struct remove_pointer { typedef T type; };
205 template struct remove_pointer { typedef T type; };
206 template struct remove_pointer { typedef T type; };
207 template struct remove_pointer { typedef T type; };
208 template struct remove_pointer {
209 typedef T type; };
210
211 // If T is a pointer, just return it. If it is not, return T&.
212 template
213 struct add_lvalue_reference_if_not_pointer { typedef T &type; };
214
215 template
216 struct add_lvalue_reference_if_not_pointer
217 typename enable_if >::type> {
218 typedef T type;
219 };
220
221 // If T is a pointer to X, return a pointer to const X. If it is not, return
222 // const T.
74 /// \brief If T is a pointer to X, return a pointer to const X. If it is not,
75 /// return const T.
22376 template
22477 struct add_const_past_pointer { typedef const T type; };
22578
226 template
227 struct add_const_past_pointer >::type> {
228 typedef const typename remove_pointer::type *type;
79 template
80 struct add_const_past_pointer<
81 T, typename std::enable_if::value>::type> {
82 typedef const typename std::remove_pointer::type *type;
22983 };
230
231 template
232 struct conditional { typedef T type; };
233
234 template
235 struct conditional { typedef F type; };
23684
23785 }
23886
115115
116116 template
117117 void CompileAssertHasType(VarType) {
118 static_assert((is_same::value), "Not the same type");
118 static_assert(std::is_same::value,
119 "Not the same type");
119120 }
120121
121122 TYPED_TEST(ValueMapTest, Iteration) {