fork download
  1.  
  2. #include <type_traits>
  3.  
  4. namespace rtl {
  5.  
  6. using true_type = std::true_type;
  7. using false_type = std::false_type;
  8.  
  9. template <typename... Types>
  10. struct type_list
  11. {
  12. };
  13.  
  14. template <typename TypeList>
  15. struct is_type_list : false_type
  16. {
  17. };
  18.  
  19. template <typename... Types>
  20. struct is_type_list<type_list<Types...>> : true_type
  21. {
  22. };
  23.  
  24. template <typename TypeList>
  25. struct empty;
  26.  
  27. template <>
  28. struct empty<type_list<>> : true_type
  29. {
  30. };
  31.  
  32. template <typename... Types>
  33. struct empty<type_list<Types...>> : false_type
  34. {
  35. };
  36.  
  37. template <typename TypeList>
  38. struct size;
  39.  
  40. template <typename... Types>
  41. struct size<type_list<Types...>> : std::integral_constant<std::size_t, sizeof...(Types)>
  42. {
  43. };
  44.  
  45. template <typename TypeList, typename T>
  46. struct push_front;
  47.  
  48. template <typename... Types, typename T>
  49. struct push_front<type_list<Types...>, T>
  50. {
  51. using type = type_list<T, Types...>;
  52. };
  53.  
  54. template <typename TypeList>
  55. struct pop_front;
  56.  
  57. template <typename... Types, typename T>
  58. struct pop_front<type_list<T, Types...>>
  59. {
  60. using type = type_list<Types...>;
  61. };
  62.  
  63. template <typename TypeList>
  64. struct front;
  65.  
  66. template <typename T, typename... Types>
  67. struct front<type_list<T, Types...>>
  68. {
  69. using type = T;
  70. };
  71.  
  72. template <typename TypeList, typename T>
  73. struct push_back;
  74.  
  75. template <typename... Types, typename T>
  76. struct push_back<type_list<Types...>, T>
  77. {
  78. using type = type_list<Types..., T>;
  79. };
  80.  
  81. template <typename TypeList>
  82. struct pop_back;
  83.  
  84. template <typename... Types, typename T>
  85. struct pop_back<type_list<Types..., T>>
  86. {
  87. using type = type_list<Types...>;
  88. };
  89.  
  90. template <typename TypeList>
  91. struct back;
  92.  
  93. template <typename... Types, typename T>
  94. struct back<type_list<Types..., T>>
  95. {
  96. using type = T;
  97. };
  98.  
  99. template <typename T>
  100. struct back<type_list<T>>
  101. {
  102. using type = T;
  103. };
  104.  
  105. /*
  106. template <typename InputTypeList, typename ResultTypeList>
  107. struct reverse_impl;
  108.  
  109. template <typename T, typename... InputTypes, typename... ResultTypes>
  110. struct reverse_impl<type_list<T, InputTypes...>, type_list<ResultTypes...>> :
  111. reverse_impl<type_list<InputTypes...>, type_list<ResultTypes..., T>>
  112. {
  113. };
  114.  
  115. template <typename... ResultTypes>
  116. struct reverse_impl<type_list<>, type_list<ResultTypes...>>
  117. {
  118. using type = type_list<ResultTypes...>;
  119. };
  120.  
  121. template <typename TypeList>
  122. struct reverse;
  123.  
  124. template <typename... Types>
  125. struct reverse<type_list<Types...>> : reverse_impl<type_list<Types...>, typelist<>>::type
  126. {
  127. };
  128. */
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. namespace detail {
  139.  
  140. template <typename TypeList, typename State, typename Operation>
  141. struct fold_impl;
  142.  
  143. template <typename State, typename Operation>
  144. struct fold_impl<
  145. type_list<>,
  146. State,
  147. Operation
  148. >
  149. {
  150. using type = State;
  151. };
  152.  
  153. template <typename T, typename... Types, typename State, typename Operation>
  154. struct fold_impl<
  155. type_list<T, Types...>,
  156. State,
  157. Operation
  158. > :
  159. fold_impl<
  160. type_list<Types...>,
  161. typename Operation::template apply<State, T>::type,
  162. Operation
  163. >
  164. {
  165. };
  166.  
  167. } // namespace detail
  168.  
  169. template <typename TypeList, typename InitialState, typename Operation>
  170. struct fold : detail::fold_impl<TypeList, InitialState, Operation>
  171. {
  172. };
  173.  
  174. template <template <typename State, typename T> class Operation>
  175. struct lambda
  176. {
  177. template <class State, class T>
  178. struct apply : Operation<State, T>
  179. {
  180. };
  181. };
  182.  
  183. template <typename TypeList>
  184. struct reverse : fold<TypeList, type_list<>, lambda<push_front>>
  185. {
  186. };
  187.  
  188. } // namespace rtl
  189.  
  190.  
  191. #include <typeinfo>
  192. #include <iostream>
  193.  
  194. struct A
  195. {
  196. };
  197.  
  198. struct B
  199. {
  200. };
  201.  
  202. struct C
  203. {
  204. };
  205.  
  206. using ABC = rtl::type_list<A, B, C>;
  207. using CBA = rtl::reverse<ABC>::type;
  208.  
  209. using ABCA = rtl::push_back<ABC, A>::type;
  210. using CABC = rtl::push_front<ABC, C>::type;
  211.  
  212. using BA = rtl::pop_front<CBA>::type;
  213. using CB = rtl::pop_back<CBA>::type;
  214.  
  215. static_assert(std::is_same<typename rtl::back<BA>::type, A>::value, "");
  216. static_assert(std::is_same<typename rtl::front<BA>::type, B>::value, "");
  217.  
  218. int main()
  219. {
  220. std::cout << typeid(BA).name() << std::endl;
  221. }
  222.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:213:32: error: ‘type’ in ‘struct rtl::pop_back<rtl::type_list<C, B, A> >’ does not name a type
 using CB = rtl::pop_back<CBA>::type;
                                ^~~~
prog.cpp:215:52: error: ‘type’ in ‘struct rtl::back<rtl::type_list<B, A> >’ does not name a type
 static_assert(std::is_same<typename rtl::back<BA>::type, A>::value, "");
                                                    ^~~~
prog.cpp:215:59: error: template argument 1 is invalid
 static_assert(std::is_same<typename rtl::back<BA>::type, A>::value, "");
                                                           ^
stdout
Standard output is empty