fork(4) download
  1. /// //////////////////////////////////////////////////////////////////////// ///
  2. /// //////////////////////////////////////////////////////////////////////// ///
  3. /// //////////////////////////////////////////////////////////////////////// ///
  4. #include <cxxabi.h>
  5. #include <string>
  6. #include <typeinfo>
  7. #include <cassert>
  8. #include <cstdlib>
  9. #include <iostream>
  10.  
  11. const std::string demangle(const char* const mangledTypeName) {
  12. int resultStatus = -4;
  13. const char* const result = abi::__cxa_demangle( mangledTypeName, 0, 0, &resultStatus );
  14. std::string demangledTypeName;
  15. const bool noError = (0 == resultStatus);
  16. const bool demangledSuccess = (noError && result != 0);
  17. assert(demangledSuccess);
  18. demangledTypeName = (demangledSuccess ? result : "Unknown type.");
  19. free(const_cast<char*>(result));
  20. return demangledTypeName;
  21. }
  22.  
  23. const std::string demangle(const std::string& mangledTypeName) {
  24. return demangle(mangledTypeName.c_str());
  25. }
  26.  
  27. const std::string demangledNameOfType(const std::type_info& type_id) {
  28. return demangle(type_id.name());
  29. }
  30.  
  31. #define DEMANGLED_NAME_OF_TYPE(TypeOrValue) (demangledNameOfType(typeid(TypeOrValue)))
  32. #define PRINT_NAME_OF_TYPE(TypeOrValue) {std::cout<<DEMANGLED_NAME_OF_TYPE(TypeOrValue)<<std::endl;}
  33. /// //////////////////////////////////////////////////////////////////////// ///
  34. /// //////////////////////////////////////////////////////////////////////// ///
  35. /// //////////////////////////////////////////////////////////////////////// ///
  36.  
  37. #include <string>
  38. #include <vector>
  39. #include <iostream>
  40.  
  41. // Store parameter pack
  42. template<typename... T>
  43. struct pack
  44. {
  45. static const unsigned int size = sizeof...(T);
  46. };
  47.  
  48. // Get i-th element of parameter pack
  49. template<int n, typename F, typename... T>
  50. struct element_at : public element_at<n-1, T...>
  51. {
  52. };
  53.  
  54. template<typename F, typename... T>
  55. struct element_at<0, F, T...>
  56. {
  57. typedef F type;
  58. };
  59.  
  60. // Get i-th element of pack
  61. template<int n, typename P>
  62. struct element
  63. {
  64. };
  65.  
  66. template<int n, typename... T>
  67. struct element<n, pack<T...>>
  68. {
  69. typedef typename element_at<n, T...>::type type;
  70. };
  71.  
  72. // Concat at left
  73. template<typename a, typename b>
  74. struct tuple_concat_left
  75. {
  76. };
  77.  
  78. template<typename a, typename... b>
  79. struct tuple_concat_left<a, pack<b...>>
  80. {
  81. typedef pack<a, b...> type;
  82. };
  83.  
  84. // Concat 2 tuples
  85. template<typename a, typename b, int n = 0, bool ok = (n < a::size)>
  86. struct tuple_concat : public tuple_concat<a, b, n+1>
  87. {
  88. typedef typename tuple_concat_left<
  89. typename element<n, a>::type,
  90. typename tuple_concat<a, b, n+1>::type
  91. >::type type;
  92. };
  93.  
  94. template<typename a, typename b, int n>
  95. struct tuple_concat<a, b, n, false>
  96. {
  97. typedef b type;
  98. };
  99.  
  100. // Test
  101. typedef pack<bool, int, char, float, std::string> MyPack;
  102. typedef pack<double, std::vector<std::string>> PackToAppend;
  103.  
  104. int main()
  105. {
  106. typedef tuple_concat_left<void, MyPack>::type concat1;
  107. typedef element<2, concat1>::type elem_at_2;
  108. typedef tuple_concat<MyPack, PackToAppend>::type concat2;
  109. typedef tuple_concat<PackToAppend, MyPack>::type concat3;
  110.  
  111.  
  112. std::cout << "MyPack: "; PRINT_NAME_OF_TYPE(MyPack);
  113. std::cout << "concat1: "; PRINT_NAME_OF_TYPE(concat1);
  114. std::cout << "elem_at_2: "; PRINT_NAME_OF_TYPE(elem_at_2);
  115. std::cout << "concat2: "; PRINT_NAME_OF_TYPE(concat2);
  116. std::cout << "concat3: "; PRINT_NAME_OF_TYPE(concat3);
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
MyPack: pack<bool, int, char, float, std::string>
concat1: pack<void, bool, int, char, float, std::string>
elem_at_2: int
concat2: pack<bool, int, char, float, std::string, double, std::vector<std::string, std::allocator<std::string> > >
concat3: pack<double, std::vector<std::string, std::allocator<std::string> >, bool, int, char, float, std::string>