简体   繁体   English

type_info不是RTTI的一部分吗?

[英]Is type_info not a part of RTTI?

I had asked a question Do C++ POD types have RTTI? 我曾问过一个问题C ++ POD类型有RTTI吗? and someone told me in the comments: 有人在评论中告诉我:

POD types do have type_info, but don't have RTTI, and that's possible because type_info isn't always RTTI. POD类型确实有type_info,但没有RTTI,这是可能的,因为type_info并不总是RTTI。

and it seems right as i could get the type_info of a POD ( non-polymorphic ) type. 它似乎正确,因为我可以得到POD( 非多态 )类型的type_info

But while I compile this simple program: 但是当我编译这个简单的程序时:

#include <iostream>

struct X
{
    int a;
};

int main()
{
    using namespace std;

    std::cout << typeid(X) << std::endl;

    return 0;
}

with flag -fno-rtti of GCC: 与GCC的旗帜-fno-rtti

$ g++ -fno-rtti main.cpp && ./main

It won't compile: 它不会编译:

main.cpp: In function ‘int main()’:
main.cpp:12:26: error: cannot use typeid with -fno-rtti
     std::cout << typeid(X) << std::endl;
                          ^

Does that mean type_info is a part of RTTI, or is it just a behavior of GCC? 这是否意味着type_info是RTTI的一部分,还是仅仅是GCC的行为

Abstract 抽象

RTTI per se is not something really formally defined: C++ only says what typeid and dynamic_cast do, not how they're implemented. RTTI本身并不是真正正式定义的东西:C ++只说明了typeiddynamic_cast作用,而不是它们是如何实现的。 However, it is convenient indeed to group such kind of operations under a common name which is RTTI. 但是,将这种操作分组为RTTI的通用名称确实很方便。

Notice an implementation is not required to strictly obtain this information at runtime ie 通知的实现并不需要严格获得在运行时也就是这个信息

if ( typeid(int) == typeid(double) )

could also be determined during the program evaluation, much like std::is_same . 也可以在程序评估期间确定,就像std::is_same int is undeniably non-polymorphic (it has no 'dynamic' type). int无可否认是非多态的(它没有'动态'类型)。 cppreference even claims: cppreference甚至声称:

When applied to an expression of polymorphic type, evaluation of a typeid expression may involve runtime overhead (a virtual table lookup), otherwise typeid expression is resolved at compile time. 当应用于多态类型的表达式时,对typeid表达式的求值可能涉及运行时开销(虚拟表查找), 否则在编译时解析typeid表达式。

But it's to be taken cautiously. 但要小心谨慎。


Does that mean type_info is a part of RTTI, or is it just a behavior of GCC? 这是否意味着type_info是RTTI的一部分,还是仅仅是GCC的行为?

type_info is a class . type_info是一个 You may not construct any object of that type - you only can through typeid . 您可能无法构造该类型的任何对象 - 您只能通过typeid

-fno-rtti disable RTTI under GCC: you can't use typeid , and thereby neither can be type_info . -fno-rtti在GCC下禁用RTTI:你不能使用typeid ,因此也不能是type_info They're very close each other. 他们彼此非常亲密。

To conclude, the original quote is totally right: 总之,原始报价是完全正确的:

POD types do have type_info , but don't have RTTI, and that's possible because type_info isn't always RTTI. POD类型确实有type_info ,但没有RTTI,这是可能的,因为type_info并不总是RTTI。

The runtime information is available through typeid . 运行时信息可通过typeid There is just nothing dynamic to consider (indeed, dynamic_cast would make no sense). 没有动态需要考虑(事实上, dynamic_cast没有任何意义)。

There is no concept of "RTTI" in the standard. 标准中没有“RTTI”的概念。 Instead, it's said in different words. 相反,用不同的话说。

  • <typeinfo> is referred to as "dynamic type identification" in [support.general] <typeinfo>在[support.general]中称为“动态类型识别”

  • [intro.object] says: "Some objects are polymorphic (10.3); the implementation generates information associated with each such object that makes it possible to determine that object's type during program execution" [intro.object]说:“有些对象是多态的 (10.3);实现生成与每个这样的对象相关的信息,这使得在程序执行期间可以确定对象的类型”

  • [expr.dynamic.cast] talks about checks that happen at "run-time". [expr.dynamic.cast]讨论在“运行时”发生的检查。 All other uses of "runtime" in the standard refer to something else. 标准中“运行时”的所有其他用法指的是其他内容。

[expr.typeid] explains what typeid does: [expr.typeid]解释了typeid作用:

2 When typeid is applied to a glvalue expression whose type is a polymorphic class type (10.3), the result refers to a std::type_info object representing the type of the most derived object (1.8) (that is, the dynamic type) to which the glvalue refers. 2当typeid应用于类型为多态类类型(10.3)的glvalue表达式时,结果引用std::type_info对象,该对象表示最派生对象(1.8)的类型(即动态类型) glvalue指的是哪个。 ... ...

The latter is typically referred to a "run-time" operation. 后者通常被称为“运行时”操作。

3 When typeid is applied to an expression other than a glvalue of a polymorphic class type, the result refers to a std::type_info object representing the static type of the expression. 3当typeid应用于多态类类型的glvalue以外的表达式时,结果引用表示表达式的静态类型的std::type_info对象。 ... The expression is an unevaluated operand (Clause 5). ......表达式是未评估的操作数(第5条)。

While the former can be seen as a "compile-time" operation. 而前者可被视为“编译时”操作。

Regardless, GCC doesn't really care and disables typeid and dynamic_cast altogether if you use the -fno-rtti flag: 无论如何,如果你使用-fno-rtti标志,GCC并不真正关心和禁用typeiddynamic_cast

rtti.c : rtti.c

  if (! flag_rtti)
    {
      error ("cannot use typeid with -fno-rtti");
      return false;
    }

Could it disable typeid for polymorphic types only? 它可以仅为多态类型禁用typeid吗? Sure. 当然。 But we're going to go with occam's razor in that it's much easier developmentally-wise to prevent the use of typeid altogether. 但我们将使用occam的剃须刀,因为在发展方面更容易防止完全使用typeid

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

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