I would like to print the type name for debugging purposes, so I created a function that does the trick (in fact, I borrowed it from another SO answer that I cannot find now), the function looks like this:
template <typename T> std::string TypeName(T) { auto name = typeid(T).name(); int status = 0; std::unique_ptr<char, void(*)(void*)> res { abi::__cxa_demangle(name, NULL, NULL, &status), std::free }; return ((status == 0) ? res.get() : name); }
Live demo
It works great:
int i = 0; float f = 0.f; std::cout << TypeName(i) << '\n'; // int std::cout << TypeName(f) << '\n'; // float, so far so good std::cout << TypeName(&i) << '\n'; // int * std::cout << TypeName(&f) << '\n'; // float *, as expected
But he has no way to handle top-level cv-cualifiers and links:
const int ci = 1; const float cf = 1.f; std::cout << TypeName(ci) << '\n'; // int! (instead of const int) std::cout << TypeName(cf) << '\n'; // float! (instead of const float) int &ri = i; float &rf = f; std::cout << TypeName(ri) << '\n'; // int! (instead of int &) std::cout << TypeName(rf) << '\n'; // float! (instead of float &)
Well, I cannot say that this is unexpected, because the TypeName function is a function template, and the T type follows the template type output, but this problem makes it all almost useless.
So my question is: is there anything that can be done to create a template function (which can get any type as input) to get the type name without losing the top levels of cv-cualifiers and links?
Thanks in advance.
source share