daily pastebin goal
27%
SHARE
TWEET

Untitled

a guest Dec 16th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdio>
  2. #include <limits>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. class NullType {};
  8.  
  9. template<typename T, typename U>
  10. class TypeList {
  11.   typedef T Head;
  12.   typedef U Tail;
  13. };
  14.  
  15. #define Typelist_1(T1) TypeList<T1, NullType>
  16. #define Typelist_2(T1, T2) TypeList<T1, Typelist_1(T2)>
  17. #define Typelist_3(T1, T2, T3) TypeList<T1, Typelist_2(T2, T3)>
  18.  
  19. typedef Typelist_1(int) int_list;
  20. typedef Typelist_3(char, unsigned char, signed char) chars;
  21.  
  22. template<typename T>
  23. struct Length;
  24.  
  25. template<>
  26. struct Length<NullType> {
  27.   enum { value = 0 };
  28. };
  29.  
  30. template<typename T1, typename T2>
  31. struct Length<TypeList<T1, T2> > {
  32.   enum { value = 1 + Length<T2>::value };
  33. };
  34.  
  35.  
  36. template<class TList, unsigned int index>
  37. struct TypeAt;
  38.  
  39. template<class T1, class T2>
  40. struct TypeAt<TypeList<T1, T2>, 0> {
  41.   typedef T1 result;
  42. };
  43.  
  44. template<class T1, class T2, unsigned int index>
  45. struct TypeAt<TypeList<T1, T2>, index> {
  46.   typedef typename TypeAt<T2, index - 1>::result result;
  47. };
  48.  
  49. typedef Typelist_3(short, int, long long) ints;
  50.  
  51. template<class TList, unsigned int index, class Default>
  52. struct TypeAtDefault;
  53.  
  54. template<unsigned int index, class Default>
  55. struct TypeAtDefault<NullType, index, Default> {
  56.   typedef Default result;
  57. };
  58.  
  59. template<class Head, class Tail, class Default>
  60. struct TypeAtDefault<TypeList<Head, Tail>, 0, Default> {
  61.   typedef Head result;
  62. };
  63.  
  64. template<class Head, class Tail, unsigned int index, class Default>
  65. struct TypeAtDefault<TypeList<Head, Tail>, index, Default> {
  66.   typedef typename TypeAtDefault<Tail, index - 1, Default>::result result;
  67. };
  68.  
  69. template<int Cond, int V1, int V2>
  70. struct StaticIf {
  71.   enum { value = V1 };
  72. };
  73.  
  74. template<int V1, int V2>
  75. struct StaticIf<0, V1, V2> {
  76.   enum { value = V2 };
  77. };
  78.  
  79. template<class TList, class Type>
  80. struct IndexOf;
  81.  
  82. template<class Type>
  83. struct IndexOf<NullType, Type> {
  84.   enum { value = -1 };
  85. };
  86.  
  87. template<class Head, class Tail>
  88. struct IndexOf<TypeList<Head, Tail>, Head> {
  89.   enum { value = 0 };
  90. };
  91.  
  92. template<class Head, class Tail, class Type>
  93. class IndexOf<TypeList<Head, Tail>, Type> {
  94.   enum { value_ = IndexOf<Tail, Type>::value };
  95. public:
  96.   enum { value = StaticIf<value_ == -1, -1, IndexOf<Tail, Type>::value + 1>::value };
  97. };
  98.  
  99. template<class TList, class Type>
  100. struct Append;
  101.  
  102. template<class Type>
  103. struct Append<NullType, Type> {
  104.   typedef Typelist_1(Type) result;
  105. };
  106.  
  107. template<class Head, class Tail, class Type>
  108. struct Append<TypeList<Head, Tail>, Type> {
  109.   typedef TypeList<Head, typename Append<Tail, Type>::result> result;
  110. };
  111.  
  112. int main() {
  113.   cout << "max val is " << numeric_limits<TypeAtDefault<ints, 3, int>::result >::max() << "\n";
  114.   cout << "index is " << IndexOf<ints, char>::value << "\n";
  115.   typedef Append<ints, double>::result intsDouble;
  116.   cout << "max val is " << numeric_limits<TypeAtDefault<intsDouble, 3, int>::result >::max() << "\n";
  117.   cout << "index is " << IndexOf<intsDouble, double>::value << "\n";
  118. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top