1 //===- llvm/Support/type_traits.h - Simplfied type traits -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides a template class that determines if a type is a class or
11 // not. The basic mechanism, based on using the pointer to member function of
12 // a zero argument to a function was "boosted" from the boost type_traits
13 // library. See http://www.boost.org/ for all the gory details.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_SUPPORT_TYPE_TRAITS_H
18 #define LLVM_SUPPORT_TYPE_TRAITS_H
20 #include "llvm/Support/DataTypes.h"
24 // This is actually the conforming implementation which works with abstract
25 // classes. However, enough compilers have trouble with it that most will use
26 // the one in boost/type_traits/object_traits.hpp. This implementation actually
27 // works with VC7.0, but other interactions seem to fail when we use it.
33 // These two functions should never be used. They are helpers to
34 // the is_class template below. They cannot be located inside
35 // is_class because doing so causes at least GCC to think that
36 // the value of the "value" enumerator is not constant. Placing
37 // them out here (for some strange reason) allows the sizeof
38 // operator against them to magically be constant. This is
39 // important to make the is_class<T>::value idiom zero cost. it
40 // evaluates to a constant 1 or 0 depending on whether the
41 // parameter T is a class or not (respectively).
42 template<typename T> char is_class_helper(void(T::*)());
43 template<typename T> double is_class_helper(...);
49 // is_class<> metafunction due to Paul Mensonides (leavings@attbi.com). For
51 // http://groups.google.com/groups?hl=en&selm=000001c1cc83%24e154d5e0%247772e50c%40c161550a&rnum=1
53 enum { value = sizeof(char) == sizeof(dont_use::is_class_helper<T>(0)) };
57 /// isPodLike - This is a type trait that is used to determine whether a given
58 /// type can be copied around with memcpy instead of running ctors etc.
61 // If we don't know anything else, we can (at least) assume that all non-class
63 static const bool value = !is_class<T>::value;
66 // std::pair's are pod-like if their elements are.
67 template<typename T, typename U>
68 struct isPodLike<std::pair<T, U> > {
69 static const bool value = isPodLike<T>::value & isPodLike<U>::value;
73 template <class T, T v>
74 struct integral_constant {
76 static const value_type value = v;
77 typedef integral_constant<T,v> type;
78 operator value_type() { return value; }
81 typedef integral_constant<bool, true> true_type;
82 typedef integral_constant<bool, false> false_type;
84 /// \brief Metafunction that determines whether the two given types are
86 template<typename T, typename U> struct is_same : public false_type {};
87 template<typename T> struct is_same<T, T> : public true_type {};
89 /// \brief Metafunction that removes const qualification from a type.
90 template <typename T> struct remove_const { typedef T type; };
91 template <typename T> struct remove_const<const T> { typedef T type; };
93 /// \brief Metafunction that removes volatile qualification from a type.
94 template <typename T> struct remove_volatile { typedef T type; };
95 template <typename T> struct remove_volatile<volatile T> { typedef T type; };
97 /// \brief Metafunction that removes both const and volatile qualification from
99 template <typename T> struct remove_cv {
100 typedef typename remove_const<typename remove_volatile<T>::type>::type type;
103 /// \brief Helper to implement is_integral metafunction.
104 template <typename T> struct is_integral_impl : false_type {};
105 template <> struct is_integral_impl< bool> : true_type {};
106 template <> struct is_integral_impl< char> : true_type {};
107 template <> struct is_integral_impl< signed char> : true_type {};
108 template <> struct is_integral_impl<unsigned char> : true_type {};
109 template <> struct is_integral_impl< wchar_t> : true_type {};
110 template <> struct is_integral_impl< short> : true_type {};
111 template <> struct is_integral_impl<unsigned short> : true_type {};
112 template <> struct is_integral_impl< int> : true_type {};
113 template <> struct is_integral_impl<unsigned int> : true_type {};
114 template <> struct is_integral_impl< long> : true_type {};
115 template <> struct is_integral_impl<unsigned long> : true_type {};
116 template <> struct is_integral_impl< long long> : true_type {};
117 template <> struct is_integral_impl<unsigned long long> : true_type {};
119 /// \brief Metafunction that determines whether the given type is an integral
121 template <typename T>
122 struct is_integral : is_integral_impl<T> {};
125 // Form a return type that can only be instantiated with an integral or enum
126 // types (or with nullptr_t in C++11).
127 template <typename U, U u = U()> struct check_nontype_temp_param_return_type {
130 template <typename U>
131 check_nontype_temp_param_return_type<U> check_nontype_temp_param(U*);
132 template <typename U> char check_nontype_temp_param(...);
134 // Form a return type that can only be instantiated with nullptr_t in C++11
135 // mode. It's harmless in C++98 mode, but this allows us to filter nullptr_t
136 // when building in C++11 mode without having to detect that mode for each
137 // different compiler.
139 template <typename U, nonce* u = U()>
140 struct check_nullptr_t_like_return_type { char c[2]; };
141 template <typename U>
142 check_nullptr_t_like_return_type<U> check_nullptr_t_like(U*);
143 template <typename U> char check_nullptr_t_like(...);
144 } // namespace dont_use
146 /// \brief Metafunction that determines whether the given type is either an
147 /// integral type or an enumeration type.
149 /// Note that this accepts potentially more integral types than we whitelist
150 /// above for is_integral, it should accept essentially anything the compiler
151 /// believes is an integral type.
152 template <typename T> struct is_integral_or_enum {
154 value = (sizeof(char) != sizeof(dont_use::check_nontype_temp_param<T>(0)) &&
155 sizeof(char) == sizeof(dont_use::check_nullptr_t_like<T>(0)))
159 /// \brief Metafunction that determines whether the given type is a pointer
161 template <typename T> struct is_pointer : false_type {};
162 template <typename T> struct is_pointer<T*> : true_type {};
164 // enable_if_c - Enable/disable a template based on a metafunction
165 template<bool Cond, typename T = void>
170 template<typename T> struct enable_if_c<false, T> { };
172 // enable_if - Enable/disable a template based on a metafunction
173 template<typename Cond, typename T = void>
174 struct enable_if : public enable_if_c<Cond::value, T> { };
177 template<typename Base> char base_of_helper(const volatile Base*);
178 template<typename Base> double base_of_helper(...);
181 /// is_base_of - Metafunction to determine whether one type is a base class of
182 /// (or identical to) another type.
183 template<typename Base, typename Derived>
185 static const bool value
186 = is_class<Base>::value && is_class<Derived>::value &&
187 sizeof(char) == sizeof(dont_use::base_of_helper<Base>((Derived*)0));
190 // remove_pointer - Metafunction to turn Foo* into Foo. Defined in
191 // C++0x [meta.trans.ptr].
192 template <typename T> struct remove_pointer { typedef T type; };
193 template <typename T> struct remove_pointer<T*> { typedef T type; };
194 template <typename T> struct remove_pointer<T*const> { typedef T type; };
195 template <typename T> struct remove_pointer<T*volatile> { typedef T type; };
196 template <typename T> struct remove_pointer<T*const volatile> {
199 template <bool, typename T, typename F>
200 struct conditional { typedef T type; };
202 template <typename T, typename F>
203 struct conditional<false, T, F> { typedef F type; };