3

I already know that it's not possible to print the name of a variable, neither in C or in C++, this is mentioned in other StackOverflow posts.

However I know that in other languages, like Delphi, it is possible to print the classname of an object. I have no idea if this is possible in C++.

Hence my question: is it possible to print the objectname of an object?

Some background: I am writing a program which is using STL vectors to keep track of locations and values (both in separate vectors), and I'd like to print the content of the vectors, preceeded by the object name (so that I can see if I'm looking at locations or if I'm looking at values).

Dominique
  • 16,450
  • 15
  • 56
  • 112

4 Answers4

7

You can use:

typeid(someObject).name();

But the return value is implementation-defined and isn't required to return anything useful or readable at all.

Returns an implementation defined null-terminated character string containing the name of the type. No guarantees are given, in particular, the returned string can be identical for several types and change between invocations of the same program.

So if you need it for more than just logging then I'd suggest to just keep track of them yourself.

More info on .name() .

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
5

This is what I use:

#include <cstddef>
#include <cstring>
#include <iostream>
#include <ostream>

    template < class T >
    std::string type_name()
    {
#ifdef __clang__
        std::string p = __PRETTY_FUNCTION__;
        return p.substr( 43, p.length() - 43 - 1 );
#elif defined( __GNUC__ )
        std::string p = __PRETTY_FUNCTION__;
#if __cplusplus < 201402
        return p.substr( 57, p.length() - 53 - 62 );
#else
        return p.substr( 46, p.length() - 46 - 1 );
#endif
#elif defined( _MSC_VER )
        std::string p = __FUNCSIG__;
        return p.substr( 38, p.length() - 38 - 7 );
#else
        return std::string("This function is not supported!");
#endif
    }

It usually gives a human friendly version of the Type. Depending on your compiler (and version) the substrings may be different.

Coliru Example: http://coliru.stacked-crooked.com/a/bcdb77a7519136ea

Trevir
  • 1,253
  • 9
  • 16
  • 3
    What if used compiler is none of these? I would add `#else` with return `"THIS FUNCTION IS NOT SUPPORTED"` or something. – xinaiz Sep 28 '16 at 17:12
1

You can use

#include <typeinfo>
typeid(object).name()

Here is a working code for the same -

#include <iostream>
#include <typeinfo>
using namespace std;

class Adi {
int a;
int b;
};

int main() {
Adi obj, obj2;
cout << typeid(obj2).name() << endl;
return 0;
}

Output : 3Adi

The output format is length of class-name followed by its name.

adisticated
  • 469
  • 2
  • 10
1

It is possible to print the name of variable using the preprocessor:

#define STRINGIFY(a)  #a
#define VAR_NAME(a)  STRINGIFY( a)

int  my_var = 42;
std::cout << "var name = " << VAR_NAME( my_var) << std::endl;

The get the name of a class you can use overloading of a template class, then you just need to implement a partial specialization of this class for each type you want to get the name of.

template< typename T> class type
{
public:
   static constexpr const char* name() {
      return "unknown";
   } // end type< T>::name
}; // type< T>

template<> class type< int>
{
public:
   static constexpr const char* name() {
      return "int";
   }
}

template<> class type< std::string>
{
public:
   static constexpr const char* name() {
      return "std::string";
   }
}

std::cout << "type name = " << type< int>::name() << std::endl;

Such classes and specializations for all POD types and STL containers is available here: https://github.com/Gemini67/Celma

Contains also a solution to get the type name of a variable.

Rene
  • 2,466
  • 1
  • 12
  • 18