fork(1) download
  1. #include <string>
  2. #include <typeinfo>
  3.  
  4. #ifdef __GNUG__
  5.  
  6. #include <cxxabi.h>
  7. #include <cstdlib>
  8. #include <memory>
  9.  
  10. template< typename T > std::string type_name()
  11. {
  12. int status ;
  13. std::unique_ptr< char[], decltype(&std::free) > buffer(
  14. __cxxabiv1::__cxa_demangle( typeid(T).name(), nullptr, 0, &status ), &std::free ) ;
  15. return status==0 ? buffer.get() : "__cxa_demangle error" ;
  16. }
  17.  
  18. #else // !defined __GNUG__
  19.  
  20. template< typename T > std::string type_name() { return typeid(T).name() ; }
  21.  
  22. #endif //__GNUG__
  23.  
  24. template< typename T > std::string type_name( const T& ) { return type_name<T>() ; }
  25.  
  26.  
  27. #define print_type_name(var) ( std::cout << #var << " is of type " << type_name(var) << "\n\n" )
  28.  
  29. template<class T> class App{
  30. public:
  31. T var;
  32. App(T arg) :var(arg){}
  33.  
  34. };
  35.  
  36. class Test{};
  37.  
  38. #include <iostream>
  39. #include <vector>
  40. #include <cstring>
  41.  
  42. int main(){
  43. print_type_name("string");
  44. print_type_name('b');
  45. print_type_name(1.1);
  46. print_type_name(1);
  47.  
  48. App<std::string> obj("obj_str");
  49. print_type_name(obj.var);
  50.  
  51. App<int> obj2(1);
  52. print_type_name(obj2.var);
  53.  
  54. Test o;
  55. App<Test> obj3(o);
  56. print_type_name(obj3.var);
  57. print_type_name(obj3);
  58.  
  59. std::vector<int> vector ;
  60. print_type_name(vector);
  61. print_type_name(vector.rbegin());
  62.  
  63. print_type_name(std::strcat);
  64. print_type_name( &std::strcat );
  65. }
  66.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
"string" is of type char [7]

'b' is of type char

1.1 is of type double

1 is of type int

obj.var is of type std::string

obj2.var is of type int

obj3.var is of type Test

obj3 is of type App<Test>

vector is of type std::vector<int, std::allocator<int> >

vector.rbegin() is of type std::reverse_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > > >

std::strcat is of type char* (char*, char const*)

&std::strcat is of type char* (*)(char*, char const*)