Home >Backend Development >C++ >How Can I Demangle Mangled C Type Names from `std::type_info`?

How Can I Demangle Mangled C Type Names from `std::type_info`?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-27 20:25:10240browse

How Can I Demangle Mangled C   Type Names from `std::type_info`?

Deciphering the Enigmatic Name from std::type_info

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).name() yields "St6vectorIiSaIiEE," rendering it decipherable only by astute minds or arcane algorithms.

Unraveling the Mystery

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:

  1. type.hpp: Establish the foundational header file with the following declaration:
#include <string>
#include <typeinfo>

std::string demangle(const char* name);

template <class T>
std::string type(const T& t) {
    return demangle(typeid(t).name());
}
  1. 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
  2. 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
  3. 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn