In the realm of C , std::type_info provides vital insights into the type of an entity, exemplified by class instances or functions. However, the mangled representation of this type name often obscures its true identity. For instance, typeid(std::vector
To tame this mangled beast, we present a solution that employs the formidable demangling capabilities of std::demangle, a powerful tool for unveiling the underlying type's human-readable form. Here's a step-by-step guide to using this solution:
#include <string> #include <typeinfo> std::string demangle(const char* name); template <class T> std::string type(const T& t) { return demangle(typeid(t).name()); }
type.cpp (C 11): Utilize C 11 features for efficient demangling operations:
#ifdef __GNUG__ #include <cstdlib> #include <memory> #include <cxxabi.h> std::string demangle(const char* name) { int status = -4; std::unique_ptr<char, void(*)(void*)> res { abi::__cxa_demangle(name, NULL, NULL, &status), std::free }; return (status==0) ? res.get() : name ; } #else std::string demangle(const char* name) { return name; } #endif
type.cpp (C 98): Provide an alternative solution for systems without C 11 features:
#ifdef __GNUG__ #include <cstdlib> #include <memory> #include <cxxabi.h> struct handle { char* p; handle(char* ptr) : p(ptr) { } ~handle() { std::free(p); } }; std::string demangle(const char* name) { int status = -4; // some arbitrary value to eliminate the compiler warning handle result( abi::__cxa_demangle(name, NULL, NULL, &status) ); return (status==0) ? result.p : name ; } #else // does nothing if not g++ std::string demangle(const char* name) { return name; } #endif
Usage: Integrate the solution into your code to effortlessly extract human-readable type names:
#include <iostream> #include "type.hpp" struct Base { virtual ~Base() {} }; struct Derived : public Base { }; int main() { Base* ptr_base = new Derived(); std::cout << "Type of ptr_base: " << type(ptr_base) << std::endl; std::cout << "Type of pointee: " << type(*ptr_base) << std::endl; delete ptr_base; }
Utilizing this approach, the following output is produced:
Type of ptr_base: Base* Type of pointee: Derived
Compatibility and Caveats:
The provided solution has been tested on various platforms and compilers, including g , clang , and Mingw32. Although it predominantly targets g , the C 98 version can be employed on non-g systems. It's essential to note that vendor-specific APIs may differ accordingly.
In conclusion, this demangling solution empowers you to effortlessly extract human-readable type names from std::type_info, eliminating the obscurity associated with mangled representations. By leveraging the provided code snippets, you can seamlessly integrate this capability into your projects, enhancing debugging and logging capabilities with clarity and ease.
The above is the detailed content of How Can I Demangle Mangled C Type Names from `std::type_info`?. For more information, please follow other related articles on the PHP Chinese website!