19
class foo
{
public:
  void say_type_name()
  {
    std::cout << typeid(this).name() << std::endl;
  }
};

int main()
{
  foo f;;
  f.say_type_name();
}

Above code prints P3foo on my ubuntu machine with g++. I am not getting why it is printing P3foo instead of just foo. If I change the code like

    std::cout << typeid(*this).name() << std::endl;

it prints 3foo.

Any thoughts?

3 Answers 3

31

Because it is a pointer to foo. And foo has 3 characters. So it becomes P3foo. The other one has type foo, so it becomes 3foo. Note that the text is implementation dependent, and in this case GCC just gives you the internal, mangled name.

Enter that mangled name into the program c++filt to get the unmangled name:

$ c++filt -t P3foo
foo*
5
  • WOW! Interesting. Two more questions.. 1 - c++filt is part of gcc distribution or part of LINUX? 2 - Is there any way to get the mangled name of a method or class? Apr 25, 2009 at 18:06
  • c++filt is part of binutils (the package where ld (the linker) and the other small utils like readelf also come from): gnu.org/software/binutils Apr 25, 2009 at 18:09
  • 5
    there is no way to do that in C++. In GCC, however there is a function exposed by the ABI that does do this. It's in cxxabi.h and called __cxa_demangle . just pass it the mangled name. Its interface follows this ABI: codesourcery.com/public/cxx-abi/abi.html#demangler Apr 25, 2009 at 18:12
  • Also remember that the result of typeid().name() is going to be implementation dependent. I do not believe that there is even a requirement that it yield something intelligible much less human readable.
    – D.Shawley
    Apr 25, 2009 at 19:00
  • 1
    visual c++, i believe, even yields a human readable string. but indeed there is no requirement for that. many things regarding type_info are unspecified (It's described in 18.5.1 in the C++ standard). Apr 25, 2009 at 19:31
19

std::type_info::name() returns an implementation specific name. AFAIK, there is no portable way to get a "nice" name, although GCC has one. Look at abi::__cxa_demangle().

int status;
char *realname = abi::__cxa_demangle(typeid(obj).name(), 0, 0, &status);
std::cout << realname;
free(realname);
1
  • Thanks for this! It was a great help in my current project. Mar 20, 2010 at 4:59
0

Is there a portable solution

workaround would be to make a template hack to return all harcoded type names as char*

which platform dont have #include <cxxabi.h>?

2
  • 1
    Without special support from the class itself (like whoami() method) any "template hack" can only output a static type, not the dynamic one (consider a pointer to polymorphic base class).
    – Ilia K.
    Oct 21, 2010 at 20:28
  • Qt provides a very nice reflection mechanism with its metaobject system. I recommend it highly. May 29, 2011 at 3:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.