简体   繁体   English

是否有一个C ++ type_info的可移植包装器,它标准化了类型名称字符串格式?

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

The format of the output of type_info::name() is implementation specific. type_info::name()的输出格式是特定于实现的。

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. 这些函数只是示例,对C ++类型系统有更好了解的人可能会设计出更好的API。 The one I'm interested in in base() . 我对base()感兴趣的那个。 All functions would raise an exception if RTTI was disabled or an unsupported compiler was detected. 如果禁用RTTI或检测到不支持的编译器,则所有函数都会引发异常。

This seems like the sort of thing that Boost might implement, but I can't find it in there anywhere. 这似乎是Boost可能实现的那种东西,但我无法在任何地方找到它。 Is there a portable library that does this? 是否有便携式库可以做到这一点?

There are some limitations to do such things in C++, so you probably won't find exactly what you want in the near future. 在C ++中执行此类操作有一些限制,因此您可能无法在不久的将来找到您想要的内容。 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. 有关编译器在编译代码中插入的类型的元信息也是编译器使用的RTL特定于实现的,因此第三方库很难在不依赖于未记录的功能的情况下做得很好在以后的版本中可能会破坏的每个特定编译器。

The Qt framework has, to my knowledge, the nearest thing to what you intended. 据我所知,Qt框架与您的意图最接近。 But they do that completely independent from RTTI. 但他们完全独立于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. 然后,您将这些模块与您的程序一起编译+链接,并使用它们的API来获取信息。 Take a look at http://doc.qt.nokia.com/latest/metaobjects.html 请查看http://doc.qt.nokia.com/latest/metaobjects.html

Jeremy Pack (from Boost Extension plugin framework) appears to have written such a thing: Jeremy Pack(来自Boost Extension插件框架)似乎写了这样的东西:

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

3. RTTI does not always function as expected across DLL boundaries. 3. RTTI并不总是在DLL边界上按预期运行。 Check out the type_info classes to see how I deal with that. 查看type_info类以了解我如何处理它。

So you could have a look there. 所以你可以看看那里。


PS. 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 这可能仍然会添加信息,所以这里是链接: https//stackoverflow.com/a/5838527/85371

GCC has __cxa_demangle https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html 海湾合作委员会有__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. 如果您定位的所有编译器都有这样的扩展,您可以使用它们编写带宏的可移植函数来检测编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM