smink smink - 1 year ago 78
C++ Question

Is it possible to print a variable's type in standard C++?

For example:

int a = 12;
cout << typeof(a) << endl;

Expected output:


Answer Source


#include <typeinfo>

// …
std::cout << typeid(a).name() << '\n';

You might have to activate RTTI in your compiler options for this to work. Additionally, the output of this depends on the compiler. It might be a raw type name or a name mangling symbol or anything in between.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download