Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3602408
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:44:32+00:00 2026-05-18T20:44:32+00:00

I’m currently working on some logging code that supposed to – among other things

  • 0

I’m currently working on some logging code that supposed to – among other things – print information about the calling function. This should be relatively easy, standard C++ has a type_info class. This contains the name of the typeid’d class/function/etc. but it’s mangled. It’s not very useful. I.e. typeid(std::vector<int>).name() returns St6vectorIiSaIiEE.

Is there a way to produce something useful from this? Like std::vector<int> for the above example. If it only works for non-template classes, that’s fine too.

The solution should work for gcc, but it would be better if I could port it. It’s for logging so it’s not so important that it can’t be turned off, but it should be helpful for debugging.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:44:33+00:00Added an answer on May 18, 2026 at 8:44 pm

    Given the attention this question / answer receives, and the valuable feedback from GManNickG, I have cleaned up the code a little bit. Two versions are given: one with C++11 features and another one with only C++98 features.

    In file type.hpp

    #ifndef TYPE_HPP
    #define TYPE_HPP
    
    #include <string>
    #include <typeinfo>
    
    std::string demangle(const char* name);
    
    template <class T>
    std::string type(const T& t) {
    
        return demangle(typeid(t).name());
    }
    
    #endif
    

    In file type.cpp (requires C++11)

    #include "type.hpp"
    #ifdef __GNUG__
    #include <cstdlib>
    #include <memory>
    #include <cxxabi.h>
    
    std::string demangle(const char* name) {
    
        int status = -4; // some arbitrary value to eliminate the compiler warning
    
        // enable c++11 by passing the flag -std=c++11 to g++
        std::unique_ptr<char, void(*)(void*)> res {
            abi::__cxa_demangle(name, NULL, NULL, &status),
            std::free
        };
    
        return (status==0) ? res.get() : name ;
    }
    
    #else
    
    // does nothing if not g++
    std::string demangle(const char* name) {
        return name;
    }
    
    #endif
    

    Usage:

    #include <iostream>
    #include "type.hpp"
    
    struct Base { virtual ~Base() {} };
    
    struct Derived : public Base { };
    
    int main() {
    
        Base* ptr_base = new Derived(); // Please use smart pointers in YOUR code!
    
        std::cout << "Type of ptr_base: " << type(ptr_base) << std::endl;
    
        std::cout << "Type of pointee: " << type(*ptr_base) << std::endl;
    
        delete ptr_base;
    }
    

    It prints:

    Type of ptr_base: Base*
    Type of pointee: Derived

    Tested with g++ 4.7.2, g++ 4.9.0 20140302 (experimental), clang++ 3.4 (trunk 184647), clang 3.5 (trunk 202594) on Linux 64 bit and g++ 4.7.2 (Mingw32, Win32 XP SP2).

    If you cannot use C++11 features, here is how it can be done in C++98, the file type.cpp is now:

    #include "type.hpp"
    #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
    


    (Update from Sep 8, 2013)

    The accepted answer (as of Sep 7, 2013), when the call to abi::__cxa_demangle() is successful, returns a pointer to a local, stack allocated array… ouch!
    Also note that if you provide a buffer, abi::__cxa_demangle() assumes it to be allocated on the heap. Allocating the buffer on the stack is a bug (from the gnu doc): “If output_buffer is not long enough, it is expanded using realloc.” Calling realloc() on a pointer to the stack… ouch! (See also Igor Skochinsky‘s kind comment.)

    You can easily verify both of these bugs: just reduce the buffer size in the accepted answer (as of Sep 7, 2013) from 1024 to something smaller, for example 16, and give it something with a name not longer than 15 (so realloc() is not called). Still, depending on your system and the compiler optimizations, the output will be: garbage / nothing / program crash.
    To verify the second bug: set the buffer size to 1 and call it with something whose name is longer than 1 character. When you run it, the program almost assuredly crashes as it attempts to call realloc() with a pointer to the stack.


    (The old answer from Dec 27, 2010)

    Important changes made to KeithB’s code: the buffer has to be either allocated by malloc or specified as NULL. Do NOT allocate it on the stack.

    It’s wise to check that status as well.

    I failed to find HAVE_CXA_DEMANGLE. I check __GNUG__ although that does not guarantee that the code will even compile. Anyone has a better idea?

    #include <cxxabi.h>
    
    const string demangle(const char* name) {
    
        int status = -4;
    
        char* res = abi::__cxa_demangle(name, NULL, NULL, &status);
    
        const char* const demangled_name = (status==0)?res:name;
    
        string ret_val(demangled_name);
    
        free(res);
    
        return ret_val;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.