Dukales

extract type from variadic template pack

Jun 19th, 2015
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #include <type_traits>
  2.  
  3. namespace
  4. {
  5.  
  6. template< std::size_t index, typename ...types >
  7. struct type_by_index
  8. {
  9.    
  10.     static_assert((index < sizeof...(types)), "index is too big");
  11.    
  12. };
  13.  
  14. template< typename first, typename ...rest >
  15. struct type_by_index< sizeof...(rest), first, rest... >
  16. {
  17.    
  18.     using type = first;
  19.    
  20. };
  21.  
  22. template< std::size_t index, typename first, typename ...rest >
  23. struct type_by_index< index, first, rest... >
  24.     : type_by_index< index, rest... >
  25. {
  26.    
  27. };
  28.  
  29. template< std::size_t index, typename ...types >
  30. using type_by_index_t = typename type_by_index< index, types... >::type;
  31.  
  32. template< typename which, typename ...where >
  33. struct index_by_type
  34. {
  35.     static_assert((0 < sizeof...(where)), "type is not contained within parameter pack");
  36.    
  37. };
  38.  
  39. template< typename which, typename ...rest >
  40. struct index_by_type< which, which, rest... >
  41.     : std::integral_constant< std::size_t, sizeof...(rest) >
  42. {
  43.    
  44. };
  45.  
  46. template< typename which, typename first, typename ...rest >
  47. struct index_by_type< which, first, rest... >
  48.     : index_by_type< which, rest... >
  49. {
  50.    
  51. };
  52.  
  53. // adaptors
  54.  
  55. template< std::size_t index, typename type >
  56. struct type_by_index_variadic;
  57.  
  58. template< std::size_t index, template< typename ...types > class type, typename ...types >
  59. struct type_by_index_variadic< index, type< types... > >
  60.     : type_by_index< index, types... >
  61. {
  62.    
  63. };
  64.  
  65. template< std::size_t index, typename type >
  66. using type_by_index_variadic_t = typename type_by_index_variadic< index, type >::type;
  67.  
  68. }
  69.  
  70. // main.cpp
  71. #include <iostream>
  72.  
  73. #include <cstdlib>
  74.  
  75. template< typename ...types >
  76. struct identity
  77. {
  78.    
  79. };
  80.  
  81. using Y = struct {};
  82. using I = identity< double, int, struct X, Y >;
  83.  
  84. static_assert(std::is_same< type_by_index_variadic_t< 0, I >, Y >{});
  85. static_assert(std::is_same< type_by_index_variadic_t< 1, I >, struct X >{});
  86. static_assert(std::is_same< type_by_index_variadic_t< 1, I >, X >{});
  87. static_assert(std::is_same< type_by_index_variadic_t< 2, I >, int >{});
  88. static_assert(std::is_same< type_by_index_variadic_t< 3, I >, double >{});
  89. static_assert(std::is_same< type_by_index_variadic_t< 0, identity< void > >, void >{});
  90.  
  91. static_assert(index_by_type< int, int, double, char >{} == 2);
  92. static_assert(index_by_type< int, int >{} == 0);
  93.  
  94. int
  95. main()
  96. {
  97.     return EXIT_SUCCESS;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment