fork(1) download
  1.  
  2. #include <iostream>
  3. #include <typeinfo>
  4. #include <string>
  5. #include <cxxabi.h>
  6.  
  7. using namespace std;
  8.  
  9. string demangle(const char* mangledName) {
  10. int status;
  11. char* result = abi::__cxa_demangle(mangledName, NULL, NULL, &status);
  12. switch(status) {
  13. case -1:
  14. cerr << "Out of memory!" << endl;
  15. exit(1);
  16. case -2:
  17. return mangledName;
  18. case -3: // Should never happen, but just in case?
  19. return mangledName;
  20. }
  21. string name = result;
  22. free(result);
  23. return name;
  24. }
  25.  
  26. template<typename T> struct preserve_qualifiers {};
  27.  
  28. template<typename T> class typeinfo {
  29. typedef preserve_qualifiers<T> wrap;
  30. public:
  31. static const string name() {
  32. string name = demangle(typeid(wrap).name());
  33. int i = name.find_first_of('<');
  34. if(i == string::npos) return name;
  35. int j = name.length() - i - 2;
  36. return name.substr(i + 1, j);
  37. }
  38. };
  39.  
  40. #define TypeOut(...) cout \
  41. << "Type " #__VA_ARGS__ ": " << endl \
  42. << " Mangled: " << typeid(__VA_ARGS__).name() << endl \
  43. << " Demangled: " << demangle(typeid(__VA_ARGS__).name()) << endl \
  44. << " typeinfo<>: " << typeinfo<__VA_ARGS__>::name() << endl
  45. class A {};
  46. template<typename T> class F {};
  47. template<int T> class G {};
  48. template<template<typename> class T> class H {};
  49. template<template<int> class T> class I {};
  50. template<typename... T> class J {};
  51. template<int... T> class K {};
  52. template<template<typename> class... T> class L {};
  53. template<template<int> class... T> class M {};
  54. template<template<typename> class... T> class N {};
  55. template<template<template<typename> class...> class... T> class O {};
  56. struct bits {int i : 4, j : 2;};
  57. template<typename T, int n> struct bits2 {T val : n;};
  58.  
  59. int main(int argc, char* argv[]) {
  60. TypeOut(void(*volatile)(void(*const)()));
  61. TypeOut(int (A::*)());
  62. TypeOut(int (A::*)()const);
  63. TypeOut(int (A::*const)());
  64. TypeOut(F<int>);
  65. TypeOut(G<3>);
  66. TypeOut(H<F>);
  67. TypeOut(I<G>);
  68. TypeOut(J<int>);
  69. TypeOut(K<3>);
  70. TypeOut(L<F>);
  71. TypeOut(M<G>);
  72. TypeOut(N<F,F,F>);
  73. TypeOut(O<N,N>);
  74. }
Success #stdin #stdout 0s 3072KB
stdin
Standard input is empty
stdout
Type void(*volatile)(void(*const)()): 
  Mangled: PFvPFvvEE
  Demangled: void (*)(void (*)())
  typeinfo<>: void (* volatile)(void (*)())
Type int (A::*)(): 
  Mangled: M1AFivE
  Demangled: int (A::*)()
  typeinfo<>: int (A::*)()
Type int (A::*)()const: 
  Mangled: M1AKFivE
  Demangled: int (A::*)() const
  typeinfo<>: int (A::*)() const
Type int (A::*const)(): 
  Mangled: M1AFivE
  Demangled: int (A::*)()
  typeinfo<>: int (A::* const)()
Type F<int>: 
  Mangled: 1FIiE
  Demangled: F<int>
  typeinfo<>: F<int> 
Type G<3>: 
  Mangled: 1GILi3EE
  Demangled: G<3>
  typeinfo<>: G<3> 
Type H<F>: 
  Mangled: 1HI1FE
  Demangled: H<F>
  typeinfo<>: H<F> 
Type I<G>: 
  Mangled: 1II1GE
  Demangled: I<G>
  typeinfo<>: I<G> 
Type J<int>: 
  Mangled: 1JIIiEE
  Demangled: J<int>
  typeinfo<>: J<int> 
Type K<3>: 
  Mangled: 1KIILi3EEE
  Demangled: K<3>
  typeinfo<>: K<3> 
Type L<F>: 
  Mangled: 1LII1FEE
  Demangled: L<F>
  typeinfo<>: L<F> 
Type M<G>: 
  Mangled: 1MII1GEE
  Demangled: M<G>
  typeinfo<>: M<G> 
Type N<F,F,F>: 
  Mangled: 1NII1FS0_S0_EE
  Demangled: N<F, F, F>
  typeinfo<>: N<F, F, F> 
Type O<N,N>: 
  Mangled: 1OII1NS0_EE
  Demangled: O<N, N>
  typeinfo<>: O<N, N>