brintos

brintos / llvm-project-archived public Read only

0
0
Text · 7.2 KiB · f1231f6 Raw
275 lines · cpp
1// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s2 3// This is a collection of various template metafunctions involving4// 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 { };17template<typename T, typename U> struct pair { };18 19namespace Count {20  template<typename Head, typename ...Tail>21  struct count {22    static const unsigned value = 1 + count<Tail...>::value;23  };24 25  template<typename T>26  struct count<T> {27    static const unsigned value = 1;28  };29 30  int check1[count<int>::value == 1? 1 : -1];31  int check2[count<float, double>::value == 2? 1 : -1];32  int check3[count<char, signed char, unsigned char>::value == 3? 1 : -1];33}34 35namespace CountWithPackExpansion {36  template<typename ...> struct count;37 38  template<typename Head, typename ...Tail>39  struct count<Head, Tail...> {40    static const unsigned value = 1 + count<Tail...>::value;41  };42 43  template<>44  struct count<> {45    static const unsigned value = 0;46  };47 48  int check0[count<>::value == 0? 1 : -1];49  int check1[count<int>::value == 1? 1 : -1];50  int check2[count<float, double>::value == 2? 1 : -1];51  int check3[count<char, signed char, unsigned char>::value == 3? 1 : -1];52}53 54namespace Replace {55  // Simple metafunction that replaces the template arguments of56  // template template parameters with 'int'.57  template<typename T>58  struct EverythingToInt;59 60  template<template<typename ...> class TT, typename T1, typename T2>61  struct EverythingToInt<TT<T1, T2> > {62    typedef TT<int, int> type;63  };64 65  int check0[is_same<EverythingToInt<tuple<double, float>>::type, 66             tuple<int, int>>::value? 1 : -1];67}68 69namespace Math {70  template<int ...Values>71  struct double_values {72    typedef int_tuple<Values*2 ...> type;73  };74 75  int check0[is_same<double_values<1, 2, -3>::type, 76                     int_tuple<2, 4, -6>>::value? 1 : -1];77 78  template<int ...Values>79  struct square {80    typedef int_tuple<(Values*Values)...> type;81  };82 83  int check1[is_same<square<1, 2, -3>::type, 84                     int_tuple<1, 4, 9>>::value? 1 : -1];85 86  template<typename IntTuple> struct square_tuple;87 88  template<int ...Values>89  struct square_tuple<int_tuple<Values...>> {90    typedef int_tuple<(Values*Values)...> type;91  };92 93  int check2[is_same<square_tuple<int_tuple<1, 2, -3> >::type, 94                     int_tuple<1, 4, 9>>::value? 1 : -1];95 96  template<int ...Values> struct sum;97 98  template<int First, int ...Rest> 99  struct sum<First, Rest...> {100    static const int value = First + sum<Rest...>::value;101  };102 103  template<>104  struct sum<> {105    static const int value = 0;106  };107 108  int check3[sum<1, 2, 3, 4, 5>::value == 15? 1 : -1];109 110  template<int ... Values>111  struct lazy_sum {112    int operator()() {113      return sum<Values...>::value;114    }115  };116 117  void f() {118    lazy_sum<1, 2, 3, 4, 5>()();119  }120}121 122namespace ListMath {123  template<typename T, T ... V> struct add;124 125  template<typename T, T i, T ... V>126  struct add<T, i, V...> {127    static const T value = i + add<T, V...>::value; 128  };129 130  template<typename T>131  struct add<T> {132    static const T value = T(); 133  };134 135  template<typename T, T ... V>136  struct List {137    struct sum {138      static const T value = add<T, V...>::value;139    };140  };141 142  template<int ... V>143  struct ListI : public List<int, V...> {144  };145 146  int check0[ListI<1, 2, 3>::sum::value == 6? 1 : -1];147}148 149namespace Indices {150  template<unsigned I, unsigned N, typename IntTuple>151  struct build_indices_impl;152 153  template<unsigned I, unsigned N, int ...Indices>154  struct build_indices_impl<I, N, int_tuple<Indices...> >155    : build_indices_impl<I+1, N, int_tuple<Indices..., I> > {156  };157 158  template<unsigned N, int ...Indices> 159  struct build_indices_impl<N, N, int_tuple<Indices...> > {160    typedef int_tuple<Indices...> type;161  };162 163  template<unsigned N>164  struct build_indices : build_indices_impl<0, N, int_tuple<> > { };165 166  int check0[is_same<build_indices<5>::type,167                     int_tuple<0, 1, 2, 3, 4>>::value? 1 : -1];168}169 170namespace TemplateTemplateApply {171  template<typename T, template<class> class ...Meta>172  struct apply_each {173    typedef tuple<typename Meta<T>::type...> type;174  };175 176  template<typename T> 177  struct add_reference {178    typedef T& type;179  };180 181  template<typename T> 182  struct add_pointer {183    typedef T* type;184  };185 186  template<typename T> 187  struct add_const {188    typedef const T type;189  };190 191  int check0[is_same<apply_each<int, 192                                add_reference, add_pointer, add_const>::type,193                     tuple<int&, int*, int const>>::value? 1 : -1];194 195  template<typename T, template<class> class ...Meta>196  struct apply_each_indirect {197    typedef typename apply_each<T, Meta...>::type type;198  };199 200  int check1[is_same<apply_each_indirect<int, add_reference, add_pointer, 201                                         add_const>::type,202                     tuple<int&, int*, int const>>::value? 1 : -1];203 204  template<typename T, typename ...Meta>205  struct apply_each_nested {206    typedef typename apply_each<T, Meta::template apply...>::type type;207  };208 209  struct add_reference_meta {210    template<typename T>211    struct apply {212      typedef T& type;213    };214  };215 216  struct add_pointer_meta {217    template<typename T>218    struct apply {219      typedef T* type;220    };221  };222 223  struct add_const_meta {224    template<typename T>225    struct apply {226      typedef const T type;227    };228  };229 230  int check2[is_same<apply_each_nested<int, add_reference_meta, 231                                       add_pointer_meta, add_const_meta>::type,232                     tuple<int&, int*, int const>>::value? 1 : -1];233 234}235 236namespace FunctionTypes {237  template<typename FunctionType>238  struct Arity;239 240  template<typename R, typename ...Types>241  struct Arity<R(Types...)> {242    static const unsigned value = sizeof...(Types);243  };244 245  template<typename R, typename ...Types>246  struct Arity<R(Types......)> { // expected-warning {{varargs}} expected-note {{pack}} expected-note {{insert ','}}247    static const unsigned value = sizeof...(Types);248  };249 250  template<typename R, typename T1, typename T2, typename T3, typename T4>251  struct Arity<R(T1, T2, T3, T4)>; // expected-note{{template is declared here}}252 253  int check0[Arity<int()>::value == 0? 1 : -1];254  int check1[Arity<int(float, double)>::value == 2? 1 : -1];255  int check2[Arity<int(float...)>::value == 1? 1 : -1];256  int check3[Arity<int(float, double, long double...)>::value == 3? 1 : -1];257  Arity<int(float, double, long double, char)> check4; // expected-error{{implicit instantiation of undefined template 'FunctionTypes::Arity<int (float, double, long double, char)>'}}258}259 260namespace SuperReplace {261  template<typename T>262  struct replace_with_int {263    typedef int type;264  };265  266  template<template<typename ...> class TT, typename ...Types>267  struct replace_with_int<TT<Types...>> {268    typedef TT<typename replace_with_int<Types>::type...> type;269  };270  271  int check0[is_same<replace_with_int<pair<tuple<float, double, short>,272                                           pair<char, unsigned char>>>::type,273                     pair<tuple<int, int, int>, pair<int, int>>>::value? 1 : -1];274}275