As a couple of other people mentioned that you can do something with RTTI, but I donβt think this is exactly what you are looking for:
#include <typeinfo> #include <iostream> #include <string> #define MY_MACRO(obj) typeid(obj).name() template<class T> class MyClass { public: MyClass(){ T b; std::cout << MY_MACRO(b) << std::endl; } }; class TestClass {}; int main(int argc, char** argv) { MyClass<std::string> a; MyClass<int> b; MyClass<TestClass> c; return 0; }
Outputs:
Ss i 9TestClass
Thanks to a comment from @MM - if you use gcc and don't mind turning a macro into a function, you can use abi::__cxa_demangle . You need to enable cxxabi.h
size_t size; int status; std::cout << abi::__cxa_demangle(typeid(obj).name(), NULL, &size, &status) << std::endl;
Output:
std::string int TestClass
source share