fork download
  1. #include <iostream>
  2. #include <typeinfo>
  3. #include <cxxabi.h>
  4. using namespace std;
  5.  
  6. // demangle from https://stackoverflow.com/questions/11347373/issue-with-type-name-demangling
  7. string demangle(const char* mangledName) {
  8. int status;
  9. char* result = abi::__cxa_demangle(mangledName, nullptr, nullptr, &status);
  10. switch(status) {
  11. case -1:
  12. cerr << "Out of memory!" << endl;
  13. exit(1);
  14. case -2:
  15. return mangledName;
  16. case -3:
  17. return mangledName;
  18. }
  19. string name = result;
  20. free(result);
  21. return name;
  22. }
  23. //
  24.  
  25. // START //
  26.  
  27.  
  28. template <typename...>
  29. struct type_list{};
  30.  
  31. template<template <typename...> typename tmpl>
  32. struct tmpl_c
  33. {
  34. template <typename...Ts> using type = tmpl<Ts...>;
  35. };
  36.  
  37. template<typename> struct template_of;
  38. template <template <typename...> typename tmpl, typename... Ts>
  39. struct template_of<tmpl<Ts...>>{
  40. using type = tmpl_c<tmpl>;
  41. };
  42.  
  43.  
  44. template <typename T>
  45. struct first{};
  46.  
  47.  
  48. template <typename T>
  49. struct second
  50. {
  51. // 'second' here refers to second<int>
  52. using test = second;
  53.  
  54. // 'second' here refers to the template second
  55. // is this due to the context? ie that the tmpl_c is expecting a template not a concrete type?
  56. using types = type_list<tmpl_c<first>, tmpl_c<second> >;
  57.  
  58.  
  59. // second here refers to the concrete type 'second<int>'
  60. // this workaround is needed for visual studio as it seems second always
  61. // refers to the concrete type, not the template.
  62. using types2 = type_list<tmpl_c<first>, typename template_of<second>::type >;
  63. };
  64.  
  65. template <typename...Ts>
  66. void print_types(type_list<Ts...>)
  67. {
  68. using do_ = int[];
  69. (void)do_{0,
  70. (std::cout << demangle(typeid(Ts).name()) << std::endl,0)
  71. ...,0};
  72. }
  73.  
  74. int main() {
  75.  
  76.  
  77. std::cout << demangle(typeid(second<int>::test).name()) << std::endl;
  78. print_types(second<int>::types());
  79. print_types(second<int>::types2());
  80. }
Success #stdin #stdout 0s 4520KB
stdin
Standard input is empty
stdout
second<int>
tmpl_c<first>
tmpl_c<second>
tmpl_c<first>
tmpl_c<second>