为什么' typeid(x)== typeid(y)'如果为' x'和' y'是类型T和T& amp; amp;的id表达式分别? [英] Why does 'typeid(x) == typeid(y)' evaluate to true, where 'x' and 'y' are id-expression of type T and T& respectively?

查看:88
本文介绍了为什么' typeid(x)== typeid(y)'如果为' x'和' y'是类型T和T& amp; amp;的id表达式分别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读C ++ 11草案标准,关于[expr.typeid]的部分提到了以下内容(强调我的意思):

I am reading the C++11 draft standard and the section on [expr.typeid] mentions the following (emphasis mine):

[...]

将typeid应用于多态类类型的glvalue以外的表达式时,结果将到表示表达式静态类型的std :: type_info对象.左值到右值(4.1),数组指针(4.2)和函数到指针(4.3)的转换不适用于该表达式.如果类型表达式是一个类类型,该类必须是完全定义的.表达式是未计算的操作数(第5条).

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. Lvalue-to-rvalue (4.1), array-topointer (4.2), and function-to-pointer (4.3) conversions are not applied to the expression. If the type of the expression is a class type, the class shall be completely-defined. The expression is an unevaluated operand (Clause 5).

将typeid应用于 type-id 时,结果引用一个std :: type_info对象,该对象表示类型ID.如果type-id的类型是对可能由cv限定的类型的引用,则typeid的结果表达式引用了一个std :: type_info对象,该对象代表了不合格的引用类型.如果type-id是类类型或对类类型的引用,则该类应完全定义.

When typeid is applied to a type-id, the result refers to a std::type_info object representing the type of the type-id. If the type of the type-id is a reference to a possibly cv-qualified type, the result of the typeid expression refers to a std::type_info object representing the cv-unqualified referenced type. If the type of the type-id is a class type or a reference to a class type, the class shall be completely-defined.

在同一部分的p5中,进一步给出以下示例:

Further in p5 of the same section, it goes on to give the following example:

class D { /* ... */ };
D d1;
const D d2;

typeid(d1) == typeid(d2); // yields true
typeid(D)  == typeid(const D); // yields true
typeid(D)  == typeid(d2); // yields true
typeid(D)  == typeid(const D&); // yields true   -- (1)


给出以下代码示例:


Given the following code sample:

int main()
{
    int foo = 42;
    int &bar = foo;
    bool comp1 = (typeid(int) == typeid(int&));    // Yields true, same as (1)   -- (2) 
    bool comp2 = (typeid(foo) == typeid(bar));     // Yields true, Why?          -- (3)
}


我的理解是 [expr.typeid] p4 仅讨论 typeid( type-id ) bar的形式 typeid(bar)中的 id-expression ,而不是 type-id .为什么上面的(3)评估为 true ?标准中的哪个文本涵盖了这一点?我错过了什么?

My understanding is that [expr.typeid]p4 talks only about the form typeid(type-id) and bar in typeid(bar) is an id-expression and not a type-id. Why does (3) above evaluate to true? Which text in the standard covers this? What have I missed?

推荐答案

答案在 [expr]

5 如果表达式最初的类型为对T"([dcl.ref],[dcl.init.ref]),将类型先调整为T分析.该表达式指定由表示的对象或功能引用,并且表达式是左值还是右值,具体取决于在表达式上.

5 If an expression initially has the type "reference to T" ([dcl.ref], [dcl.init.ref]), the type is adjusted to T prior to any further analysis. The expression designates the object or function denoted by the reference, and the expression is an lvalue or an xvalue, depending on the expression.

因此,当我们进入 [expr.typeid]

3 当typeid应用于其他表达式时而不是一个多态类类型,结果引用一个std :: type_info对象表示表达式的静态类型.左值到右值([conv.lval]),数组到指针([conv.array])和函数到指针([conv.func])转换不适用于表达.如果表达式的类型是类类型,则该类应该是完全定义的.表达式是未计算的操作数(子句[expr]).

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. Lvalue-to-rvalue ([conv.lval]), array-to-pointer ([conv.array]), and function-to-pointer ([conv.func]) conversions are not applied to the expression. If the type of the expression is a class type, the class shall be completely-defined. The expression is an unevaluated operand (Clause [expr]).

typeid 检查它时,所讨论的id表达式已具有引用的类型.

The id-expression in question is already of the referenced type when typeid examines it.

这篇关于为什么' typeid(x)== typeid(y)'如果为' x'和' y'是类型T和T& amp; amp;的id表达式分别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆