05 January 2009

Class Names under gcc

We recently changed our C++ framework to use the language’s RTTI (historically we didn’t because some compilers didn’t do a good job with it, but that’s pretty ancient history by now). Previously we used a bunch of macros to roll our own, including the ability to print an object’s class (for debugging purposes), something like “ResourceImage.” The first cut at “native” support showed this as “N3GF213ResourceImageE,” which is too ugly for my liking. (It’s essentially a mangled name.)

So Chris Blackwell pointed me in the right direction, and we now have
#include <cxxabi.h>

template <typename ObjType> std::string class_name(ObjType* anObject)
int status;
char *realName;
realName = abi::__cxa_demangle(typeid(*anObject).name(), 0, 0, &status);
std::string retVal(realName);
return retVal;
which returns a human-readable name.

No comments: