fork 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. #include <iostream>
  30.  
  31. int main()
  32. {
  33. int array2D[3][3] = { { 1, 5, 6 }, { 45, 65, 65}, { 78, 10, 99 } } ;
  34.  
  35. auto ptr = array2D+1 ;
  36.  
  37. print_type_name( ptr ) ; // ptr is of type int (*) [3]
  38.  
  39. print_type_name( *ptr ) ; // *ptr is of type int [3]
  40.  
  41. }
  42.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
ptr is of type int (*) [3]

*ptr is of type int [3]