Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a portable wrapper for C++ type_info that standardizes type name string format?

Tags:

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?

like image 595
paperjam Avatar asked Dec 12 '11 13:12

paperjam


2 Answers

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

like image 197
Fabio Ceconello Avatar answered Sep 28 '22 04:09

Fabio Ceconello


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

like image 42
sehe Avatar answered Sep 28 '22 04:09

sehe