lundi 1 juin 2020

Why does 'typeid(x) == typeid(y)' evaluates to true, where 'x' and 'y' are id-expression of type T and T& respectively?

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

[...]

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).

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.

Further in p5 of same section, it goes on to give 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)


My question is given 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)
}


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

Aucun commentaire:

Enregistrer un commentaire