Here is my C ++ 11 implementation, obtained on the following page: http://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html
#include <cxxabi.h> // needed for abi::__cxa_demangle std::shared_ptr<char> cppDemangle(const char *abiName) { int status; char *ret = abi::__cxa_demangle(abiName, 0, 0, &status); /* NOTE: must free() the returned char when done with it! */ std::shared_ptr<char> retval; retval.reset( (char *)ret, [](char *mem) { if (mem) free((void*)mem); } ); return retval; }
To simplify memory management on returned (char *), I use std :: shared_ptr with the lambda 'deleter' function, which calls free () in the returned memory. Because of this, I donβt have to worry about deleting the memory myself, I just use it as needed, and when shared_ptr goes out of scope, the memory will be free.
Here's the macro that I use to access the name of a demangled name of type (const char *). Please note that you must enable RTTI to access "typeid"
#define CLASS_NAME(somePointer) ((const char *) cppDemangle(typeid(*somePointer).name()).get() )
So, from within the C ++ class, I can say:
printf("I am inside of a %s\n",CLASS_NAME(this));
Dave
source share