21

The format of the output of type_info::name() is implementation specific.

namespace N { struct A; }

const N::A *a;

typeid(a).name(); // returns e.g. "const struct N::A" but compiler-specific

Has anyone written a wrapper that returns dependable, predictable type information that is the same across compilers. Multiple templated functions would allow user to get specific information about a type. So I might be able to use:

MyTypeInfo::name(a); // returns "const struct N::A *"
MyTypeInfo::base(a); // returns "A"
MyTypeInfo::pointer(a); // returns "*"
MyTypeInfo::nameSpace(a); // returns "N"
MyTypeInfo::cv(a); // returns "const"

These functions are just examples, someone with better knowledge of the C++ type system could probably design a better API. The one I'm interested in in base(). All functions would raise an exception if RTTI was disabled or an unsupported compiler was detected.

This seems like the sort of thing that Boost might implement, but I can't find it in there anywhere. Is there a portable library that does this?

paperjam
  • 7,556
  • 11
  • 45
  • 71
  • I'm not aware of anything, but it sounds like it would be a good idea. (Formally, the standard allows an implementation to return `""` for all types, but practically, most implementations return something directly usable, and the ones that don't return the mangled name, which can be demangled.) – James Kanze Dec 12 '11 at 15:00
  • 1
    The only issue I can see is that the string is completely implementation specific, and by no mean guaranteed to faithfully represent the type itself. Even worse, it might not even be unique. – Matthieu M. Dec 12 '11 at 20:32
  • @Matthieu - library detects such compilers and the call fails. Caller has to live with it. The compilers most of us use give useful information in `type_info`. – paperjam Dec 12 '11 at 20:39
  • possible duplicate of [Why does typeid.name() return weird characters using GCC and how to make it print unmangled names?](http://stackoverflow.com/questions/4465872/why-does-typeid-name-return-weird-characters-using-gcc-and-how-to-make-it-prin) – Ciro Santilli新疆棉花TRUMP BAN BAD Jun 18 '15 at 15:03

3 Answers3

2

There are some limitations to do such things in C++, so you probably won't find exactly what you want in the near future. The meta-information about the types that the compiler inserts in the compiled code is also implementation-specific to the RTL used by the compiler, so it'd be difficult for a third-party library to do a good job without relying to undocumented features of each specific compiler that might break in later versions.

The Qt framework has, to my knowledge, the nearest thing to what you intended. But they do that completely independent from RTTI. Instead, they have their own "compiler" that parses the source code and generates additional source modules with the meta-information. Then, you compile+link these modules along with your program and use their API to get the information. Take a look at http://doc.qt.nokia.com/latest/metaobjects.html

Fabio Ceconello
  • 15,049
  • 5
  • 34
  • 49
  • I don't see much of a problem. Having something which works at least with *some* compilers is better than nothing, imho. – zerm Dec 12 '11 at 14:52
  • I think this can be made robust on all compilers because self tests are trivial with low runtime overhead. – paperjam Dec 12 '11 at 18:49
1

Jeremy Pack (from Boost Extension plugin framework) appears to have written such a thing:

http://blog.redshoelace.com/2009/06/resource-management-across-dll.html

 3. RTTI does not always function as expected across DLL boundaries. Check out the type_info classes to see how I deal with that.

So you could have a look there.


PS. I remembered because I once fixed a bug in that area; this might still add information so here's the link: https://stackoverflow.com/a/5838527/85371

Community
  • 1
  • 1
sehe
  • 328,274
  • 43
  • 416
  • 565
  • I'm not sure this does quite what I'm asking - I'm interested in the actual type name string, not comparing types. – paperjam Dec 18 '11 at 22:29
0

GCC has __cxa_demangle https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html

If there are such extensions for all compilers you target, you could use them to write a portable function with macros to detect the compiler.