Advertisement
fpelliccioni

Variadic to Head/Tail Generator

Apr 25th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <cstring>
  2.  
  3. namespace my_types
  4. {
  5.  
  6. struct nil {};
  7.  
  8. template <typename T, typename U>
  9. struct chain
  10. {
  11.     typedef T first_type;
  12.     typedef U second_type;
  13. };
  14.  
  15. }
  16.  
  17. template <template <typename, typename> class Chain, typename Nil>
  18. struct chain_generator
  19. {
  20.     template <typename T, typename... Args>
  21.     struct gen
  22.     {
  23.     private:
  24.         template <size_t S = sizeof...(Args), typename Dummy = void>
  25.         struct inner
  26.         {
  27.             typedef Chain<T, typename gen<Args...>::type > type;
  28.         };
  29.  
  30.         template <typename Dummy>
  31.         struct inner<0, Dummy>
  32.         {
  33.             typedef Chain<T, Nil> type;
  34.         };
  35.     public:
  36.         typedef typename inner<sizeof...(Args)>::type type;
  37.     };
  38. };
  39.  
  40.  
  41. int main( /* int argc, char* argv[] */ )
  42. {
  43.     using my_types::chain;
  44.     using my_types::nil;
  45.  
  46.     typedef chain<int, chain<float, chain<char, nil>>> t0;
  47.  
  48.     typedef chain_generator<chain, nil> my_gen;
  49.  
  50.     typedef my_gen::gen<int, float, char>::type t1;
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement