fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. template <typename>
  5. struct typeinfo;
  6.  
  7. #define DEFINE_TYPEINFO_0(t, v) \
  8.   template <> \
  9.   struct typeinfo<t> \
  10.   { \
  11.   static std::string name() { return v; } \
  12.   };
  13.  
  14. #define DEFINE_TYPEINFO_1(t, v) \
  15.   template <typename T> \
  16.   struct typeinfo<t> \
  17.   { \
  18.   static std::string name() { return v; } \
  19.   };
  20.  
  21. #define TYPEINFO(x) typeinfo<x>::name()
  22.  
  23. DEFINE_TYPEINFO_0(int, "int")
  24. DEFINE_TYPEINFO_0(float, "float")
  25.  
  26. DEFINE_TYPEINFO_1(T*, TYPEINFO(T) + "*")
  27.  
  28. namespace foo
  29. {
  30. struct bar
  31. {};
  32. }
  33.  
  34. DEFINE_TYPEINFO_0(foo::bar, "foo::bar")
  35.  
  36. int main()
  37. {
  38. std::cout << TYPEINFO(int) << '\n';
  39. std::cout << TYPEINFO(float*) << '\n';
  40. std::cout << TYPEINFO(foo::bar) << '\n';
  41. }
Success #stdin #stdout 0s 3016KB
stdin
Standard input is empty
stdout
int
float*
foo::bar