metafunctions.cpp revision 61c4d28e36cd3f1be392cb77f07436d1fa6b0f9f
1// RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s 2 3// This is a collection of various template metafunctions involving 4// variadic templates, which are meant to exercise common use cases. 5template<typename T, typename U> 6struct is_same { 7 static const bool value = false; 8}; 9 10template<typename T> 11struct is_same<T, T> { 12 static const bool value = true; 13}; 14 15template<typename...> struct tuple { }; 16template<int ...> struct int_tuple { }; 17 18namespace Count { 19 template<typename Head, typename ...Tail> 20 struct count { 21 static const unsigned value = 1 + count<Tail...>::value; 22 }; 23 24 template<typename T> 25 struct count<T> { 26 static const unsigned value = 1; 27 }; 28 29 int check1[count<int>::value == 1? 1 : -1]; 30 int check2[count<float, double>::value == 2? 1 : -1]; 31 int check3[count<char, signed char, unsigned char>::value == 3? 1 : -1]; 32} 33 34namespace CountWithPackExpansion { 35 template<typename ...> struct count; 36 37 template<typename Head, typename ...Tail> 38 struct count<Head, Tail...> { 39 static const unsigned value = 1 + count<Tail...>::value; 40 }; 41 42 template<> 43 struct count<> { 44 static const unsigned value = 0; 45 }; 46 47 int check0[count<>::value == 0? 1 : -1]; 48 int check1[count<int>::value == 1? 1 : -1]; 49 int check2[count<float, double>::value == 2? 1 : -1]; 50 int check3[count<char, signed char, unsigned char>::value == 3? 1 : -1]; 51} 52 53namespace Replace { 54 // Simple metafunction that replaces the template arguments of 55 // template template parameters with 'int'. 56 template<typename T> 57 struct EverythingToInt; 58 59 template<template<typename ...> class TT, typename T1, typename T2> 60 struct EverythingToInt<TT<T1, T2> > { 61 typedef TT<int, int> type; 62 }; 63 64 int check0[is_same<EverythingToInt<tuple<double, float>>::type, 65 tuple<int, int>>::value? 1 : -1]; 66} 67 68namespace Math { 69 template<int ...Values> 70 struct double_values { 71 typedef int_tuple<Values*2 ...> type; 72 }; 73 74 int check0[is_same<double_values<1, 2, -3>::type, 75 int_tuple<2, 4, -6>>::value? 1 : -1]; 76 77 template<int ...Values> 78 struct square { 79 typedef int_tuple<(Values*Values)...> type; 80 }; 81 82 int check1[is_same<square<1, 2, -3>::type, 83 int_tuple<1, 4, 9>>::value? 1 : -1]; 84 85 template<typename IntTuple> struct square_tuple; 86 87 template<int ...Values> 88 struct square_tuple<int_tuple<Values...>> { 89 typedef int_tuple<(Values*Values)...> type; 90 }; 91 92 int check2[is_same<square_tuple<int_tuple<1, 2, -3> >::type, 93 int_tuple<1, 4, 9>>::value? 1 : -1]; 94 95 template<int ...Values> struct sum; 96 97 template<int First, int ...Rest> 98 struct sum<First, Rest...> { 99 static const int value = First + sum<Rest...>::value; 100 }; 101 102 template<> 103 struct sum<> { 104 static const int value = 0; 105 }; 106 107 int check3[sum<1, 2, 3, 4, 5>::value == 15? 1 : -1]; 108 109 template<int ... Values> 110 struct lazy_sum { 111 int operator()() { 112 return sum<Values...>::value; 113 } 114 }; 115 116 void f() { 117 lazy_sum<1, 2, 3, 4, 5>()(); 118 } 119} 120 121namespace Indices { 122 template<unsigned I, unsigned N, typename IntTuple> 123 struct build_indices_impl; 124 125 template<unsigned I, unsigned N, int ...Indices> 126 struct build_indices_impl<I, N, int_tuple<Indices...> > 127 : build_indices_impl<I+1, N, int_tuple<Indices..., I> > { 128 }; 129 130 template<unsigned N, int ...Indices> 131 struct build_indices_impl<N, N, int_tuple<Indices...> > { 132 typedef int_tuple<Indices...> type; 133 }; 134 135 template<unsigned N> 136 struct build_indices : build_indices_impl<0, N, int_tuple<> > { }; 137 138 int check0[is_same<build_indices<5>::type, 139 int_tuple<0, 1, 2, 3, 4>>::value? 1 : -1]; 140} 141 142namespace TemplateTemplateApply { 143 template<typename T, template<class> class ...Meta> 144 struct apply_each { 145 typedef tuple<typename Meta<T>::type...> type; 146 }; 147 148 template<typename T> 149 struct add_reference { 150 typedef T& type; 151 }; 152 153 template<typename T> 154 struct add_pointer { 155 typedef T* type; 156 }; 157 158 template<typename T> 159 struct add_const { 160 typedef const T type; 161 }; 162 163 int check0[is_same<apply_each<int, 164 add_reference, add_pointer, add_const>::type, 165 tuple<int&, int*, int const>>::value? 1 : -1]; 166} 167