smink smink - 2 months ago 13
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:

int

Answer

Try:

#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.