fork download
  1. #include <type_traits>
  2.  
  3. namespace tmpalg {
  4. template <typename... T> struct type_list;
  5. template <typename T> using identity = T;
  6.  
  7. template <typename TL>
  8. using size = std::integral_constant<int, -1>; // size::value contains the number
  9. // of types in type_list<T...>
  10.  
  11. template <typename TL, int N>
  12. using at = void; // Nth type in TL=type_list<T...> (zero based)
  13.  
  14. template <typename TL, template <typename> class F>
  15. using transform = void; // type_list<T...> becomes type_list<F<T>...>
  16.  
  17. template <typename TL, template <typename> class Pred=identity>
  18. using filter = void; // returns a type_list where for every element T
  19. // Pred<T>::value == true holds
  20.  
  21. template <typename TL, template <typename> class Pred=identity>
  22. using all_of = std::false_type; // all_of<...>::value == true if
  23. // Pred<T>::value == true for all T in TL
  24.  
  25. template <typename TL, template <typename> class Pred=identity>
  26. using any_of = std::false_type; // all_of<...>::value == true if
  27. // Pred<T>::value == true for at least one T in TL
  28.  
  29. template <typename TL, template <typename> class Pred=identity>
  30. using none_of = std::false_type; // all_of<...>::value == true if
  31. // Pred<T>::value == true for no T in TL
  32. }
  33.  
  34. template <typename T> struct test;
  35.  
  36. int main()
  37. {
  38. using namespace tmpalg;
  39.  
  40. using list = type_list<int /*0*/, char /*1*/, long /*2*/,
  41. float /*3*/, short /*4*/, void /*5*/,
  42. double /*6*/, int /*7*/>;
  43. static_assert(size<list>::value == 8, "size fails");
  44. static_assert(size<type_list<> >::value == 0, "size fails");
  45. static_assert(std::is_same<at<list, 3>, float>::value, "at fails");
  46. static_assert(std::is_same<transform<list, identity>, list>::value, "transform fails");
  47. static_assert(std::is_same<at<transform<list, test>, 1>, test<char> >::value, "transform fails");
  48. static_assert(size<transform<list, test> >::value == size<list>::value, "transform fails");
  49. static_assert(size<filter<list, std::is_integral> >::value == size<list>::value - 3, "filter fails");
  50. static_assert(all_of<filter<list, std::is_integral>, std::is_integral>::value, "all_of fails");
  51. static_assert(!all_of<list, std::is_integral>::value, "all_of fails");
  52. static_assert(any_of<list, std::is_integral>::value, "any_of fails");
  53. static_assert(!any_of<list, std::is_pointer>::value, "any_of fails");
  54. static_assert(none_of<list, std::is_pointer>::value, "none_of fails");
  55. static_assert(!none_of<list, std::is_fundamental>::value, "none_of fails");
  56.  
  57. /* these lines should not compile (if you remove the comments) */
  58. //using X = at<999, list>;
  59. //using Y = at<0, void>;
  60. //auto Z = size<void>::value;
  61. }
  62.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty