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 // This is actually the conforming implementation which works with abstract
21 // classes. However, enough compilers have trouble with it that most will use
22 // the one in boost/type_traits/object_traits.hpp. This implementation actually
23 // works with VC7.0, but other interactions seem to fail when we use it.
29 // These two functions should never be used. They are helpers to
30 // the is_class template below. They cannot be located inside
31 // is_class because doing so causes at least GCC to think that
32 // the value of the "value" enumerator is not constant. Placing
33 // them out here (for some strange reason) allows the sizeof
34 // operator against them to magically be constant. This is
35 // important to make the is_class<T>::value idiom zero cost. it
36 // evaluates to a constant 1 or 0 depending on whether the
37 // parameter T is a class or not (respectively).
38 template<typename T> char is_class_helper(void(T::*)());
39 template<typename T> double is_class_helper(...);
45 // is_class<> metafunction due to Paul Mensonides (leavings@attbi.com). For
47 // http://groups.google.com/groups?hl=en&selm=000001c1cc83%24e154d5e0%247772e50c%40c161550a&rnum=1
49 enum { value = sizeof(char) == sizeof(dont_use::is_class_helper<T>(0)) };
52 /// \brief Metafunction that determines whether the two given types are
54 template<typename T, typename U>
56 static const bool value = false;
60 struct is_same<T, T> {
61 static const bool value = true;
64 // enable_if_c - Enable/disable a template based on a metafunction
65 template<bool Cond, typename T = void>
70 template<typename T> struct enable_if_c<false, T> { };
72 // enable_if - Enable/disable a template based on a metafunction
73 template<typename Cond, typename T = void>
74 struct enable_if : public enable_if_c<Cond::value, T> { };
77 template<typename Base> char base_of_helper(const volatile Base*);
78 template<typename Base> double base_of_helper(...);
81 /// is_base_of - Metafunction to determine whether one type is a base class of
82 /// (or identical to) another type.
83 template<typename Base, typename Derived>
85 static const bool value
86 = is_class<Base>::value && is_class<Derived>::value &&
87 sizeof(char) == sizeof(dont_use::base_of_helper<Base>((Derived*)0));
90 // remove_pointer - Metafunction to turn Foo* into Foo. Defined in
91 // C++0x [meta.trans.ptr].
92 template <typename T> struct remove_pointer { typedef T type; };
93 template <typename T> struct remove_pointer<T*> { typedef T type; };
94 template <typename T> struct remove_pointer<T*const> { typedef T type; };
95 template <typename T> struct remove_pointer<T*volatile> { typedef T type; };
96 template <typename T> struct remove_pointer<T*const volatile> {
99 template <bool, typename T, typename F>
100 struct conditional { typedef T type; };
102 template <typename T, typename F>
103 struct conditional<false, T, F> { typedef F type; };