fork(4) download
  1. #include <iostream>
  2. #include <type_traits>
  3. #include <typeinfo>
  4.  
  5. template<typename ...Args>
  6. struct holder;
  7.  
  8. namespace
  9. {
  10. template<typename ...Args>
  11. struct print_helper;
  12.  
  13. template<typename Arg, typename ...Args>
  14. struct print_helper<Arg, Args...>
  15. {
  16. static void print()
  17. {
  18. std::cout << typeid(Arg).name() << ", ";
  19. print_helper<Args...>::print();
  20. }
  21. };
  22.  
  23. template<typename Arg>
  24. struct print_helper<Arg>
  25. {
  26. static void print()
  27. {
  28. std::cout << typeid(Arg).name() << std::endl;
  29. }
  30. };
  31.  
  32. template<typename ...Args>
  33. struct print_helper<holder<Args...>>
  34. {
  35. static void print()
  36. {
  37. print_helper<Args...>::print();
  38. }
  39. };
  40. }
  41.  
  42. template<typename ...Args>
  43. void print()
  44. {
  45. print_helper<Args...>::print();
  46. }
  47.  
  48. namespace
  49. {
  50. template<typename Applier, typename T>
  51. struct apply_helper
  52. {
  53. typedef typename Applier::template apply<T>::type type;
  54. };
  55.  
  56. template<typename Applier, typename ... Args>
  57. struct transform_helper
  58. {
  59. typedef holder<typename apply_helper<Applier, Args>::type...> type;
  60. };
  61. }
  62.  
  63. template<typename Applier, typename ... Args>
  64. struct transform
  65. {
  66. typedef typename transform_helper<Applier, Args...>::type type;
  67. };
  68.  
  69. template<typename Applier, typename ... Args>
  70. struct transform<Applier, holder<Args...>>
  71. {
  72. typedef typename transform<Applier, Args...>::type type;
  73. };
  74.  
  75. namespace
  76. {
  77. template<typename T>
  78. struct type_replacer_helper
  79. {
  80. typedef T type;
  81. };
  82.  
  83. template<>
  84. struct type_replacer_helper<int>
  85. {
  86. typedef float type;
  87. };
  88. }
  89.  
  90. struct type_replacer
  91. {
  92. template<typename T>
  93. struct apply
  94. {
  95. typedef typename type_replacer_helper<T>::type type;
  96. };
  97. };
  98.  
  99. using namespace std;
  100.  
  101. int main() {
  102.  
  103. typedef holder<int, int, char, int, float> t1;
  104. typedef typename transform<type_replacer, t1>::type t2;
  105.  
  106. print<t1>();
  107. print<t2>();
  108.  
  109. return 0;
  110. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
i, i, c, i, f
f, f, c, f, f