fork download
  1. #include <type_traits>
  2.  
  3. template <typename...>
  4. struct typelist {};
  5.  
  6. template <typename, int>
  7. struct pair {};
  8.  
  9. template <int... I>
  10. struct indices {
  11. typedef indices<I..., sizeof...(I)> next;
  12. };
  13.  
  14. template <int N>
  15. struct indices_upto {
  16. typedef typename indices_upto<N-1>::type::next type;
  17. };
  18. template <>
  19. struct indices_upto<0> {
  20. typedef indices<> type;
  21. };
  22.  
  23. template <typename T>
  24. struct indices_for;
  25. template <template <typename...> class TT, typename... T>
  26. struct indices_for<TT<T...>> : indices_upto<sizeof...(T)> {};
  27.  
  28. template <typename, typename>
  29. struct zip;
  30. template <template <typename...> class TT, typename... T, template <int...> class TI, int... I>
  31. struct zip<TT<T...>, TI<I...>> {
  32. static_assert(sizeof...(T) == sizeof...(I), "sizes must match");
  33. typedef typelist<pair<T,I>...> type;
  34. };
  35.  
  36.  
  37. template <int I, typename List>
  38. struct element_impl;
  39.  
  40. template <typename... TL, int... IL, typename T, int I, typename... TR, int... IR>
  41. struct element_impl<I, typelist<pair<TL,IL>..., pair<T,I>, pair<TR,IR>...>> {
  42. typedef T type;
  43. };
  44.  
  45. template <int I, typename List>
  46. struct element
  47. : element_impl<I, typename zip<List, typename indices_for<List>::type>::type> {};
  48.  
  49. #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
  50.  
  51. typedef typelist<int, double, char, float, long> list;
  52. typedef typename element<0, list>::type should_be_int;
  53. typedef typename element<1, list>::type should_be_double;
  54. typedef typename element<2, list>::type should_be_char;
  55. typedef typename element<3, list>::type should_be_float;
  56. typedef typename element<4, list>::type should_be_long;
  57.  
  58. STATIC_ASSERT(std::is_same<should_be_int, int>::value);
  59. STATIC_ASSERT(std::is_same<should_be_double, double>::value);
  60. STATIC_ASSERT(std::is_same<should_be_char, char>::value);
  61. STATIC_ASSERT(std::is_same<should_be_float, float>::value);
  62. STATIC_ASSERT(std::is_same<should_be_long, long>::value);
  63.  
  64. int main() {}
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of 'element<0, typelist<int, double, char, float, long int> >':
prog.cpp:52:34:   instantiated from here
prog.cpp:47:79: error: invalid use of incomplete type 'struct element_impl<0, typelist<pair<int, 0>, pair<double, 1>, pair<char, 2>, pair<float, 3>, pair<long int, 4> > >'
prog.cpp:38:8: error: declaration of 'struct element_impl<0, typelist<pair<int, 0>, pair<double, 1>, pair<char, 2>, pair<float, 3>, pair<long int, 4> > >'
prog.cpp:52:36: error: 'type' in class 'element<0, typelist<int, double, char, float, long int> >' does not name a type
prog.cpp:52:54: error: invalid type in declaration before ';' token
prog.cpp: In instantiation of 'element<1, typelist<int, double, char, float, long int> >':
prog.cpp:53:34:   instantiated from here
prog.cpp:47:79: error: invalid use of incomplete type 'struct element_impl<1, typelist<pair<int, 0>, pair<double, 1>, pair<char, 2>, pair<float, 3>, pair<long int, 4> > >'
prog.cpp:38:8: error: declaration of 'struct element_impl<1, typelist<pair<int, 0>, pair<double, 1>, pair<char, 2>, pair<float, 3>, pair<long int, 4> > >'
prog.cpp:53:36: error: 'type' in class 'element<1, typelist<int, double, char, float, long int> >' does not name a type
prog.cpp:53:57: error: invalid type in declaration before ';' token
prog.cpp: In instantiation of 'element<2, typelist<int, double, char, float, long int> >':
prog.cpp:54:34:   instantiated from here
prog.cpp:47:79: error: invalid use of incomplete type 'struct element_impl<2, typelist<pair<int, 0>, pair<double, 1>, pair<char, 2>, pair<float, 3>, pair<long int, 4> > >'
prog.cpp:38:8: error: declaration of 'struct element_impl<2, typelist<pair<int, 0>, pair<double, 1>, pair<char, 2>, pair<float, 3>, pair<long int, 4> > >'
prog.cpp:54:36: error: 'type' in class 'element<2, typelist<int, double, char, float, long int> >' does not name a type
prog.cpp:54:55: error: invalid type in declaration before ';' token
prog.cpp: In instantiation of 'element<3, typelist<int, double, char, float, long int> >':
prog.cpp:55:34:   instantiated from here
prog.cpp:47:79: error: invalid use of incomplete type 'struct element_impl<3, typelist<pair<int, 0>, pair<double, 1>, pair<char, 2>, pair<float, 3>, pair<long int, 4> > >'
prog.cpp:38:8: error: declaration of 'struct element_impl<3, typelist<pair<int, 0>, pair<double, 1>, pair<char, 2>, pair<float, 3>, pair<long int, 4> > >'
prog.cpp:55:36: error: 'type' in class 'element<3, typelist<int, double, char, float, long int> >' does not name a type
prog.cpp:55:56: error: invalid type in declaration before ';' token
prog.cpp: In instantiation of 'element<4, typelist<int, double, char, float, long int> >':
prog.cpp:56:34:   instantiated from here
prog.cpp:47:79: error: invalid use of incomplete type 'struct element_impl<4, typelist<pair<int, 0>, pair<double, 1>, pair<char, 2>, pair<float, 3>, pair<long int, 4> > >'
prog.cpp:38:8: error: declaration of 'struct element_impl<4, typelist<pair<int, 0>, pair<double, 1>, pair<char, 2>, pair<float, 3>, pair<long int, 4> > >'
prog.cpp:56:36: error: 'type' in class 'element<4, typelist<int, double, char, float, long int> >' does not name a type
prog.cpp:56:55: error: invalid type in declaration before ';' token
prog.cpp:59:1: error: static assertion failed: "std::is_same<should_be_double, double>::value"
prog.cpp:60:1: error: static assertion failed: "std::is_same<should_be_char, char>::value"
prog.cpp:61:1: error: static assertion failed: "std::is_same<should_be_float, float>::value"
prog.cpp:62:1: error: static assertion failed: "std::is_same<should_be_long, long>::value"
stdout
Standard output is empty