Type_info ignores cv classifiers: is that right?

Is this the correct behavior or is it a g ++ 4.5 quirk that this code prints 1?

#include <iostream>
#include <typeinfo>
using namespace std;

int main(){
    struct A{};
    cout<<(typeid(A)==typeid(const A)&&typeid(A)==typeid(const volatile A)&&typeid(A)==typeid(volatile A));
}

      

I thought that types differing for cv qualifiers were treated as very different types, even though less qualified cv types could be implicitly cast to more qualified types.

+3


source to share


1 answer


typeid

ignores cv qualifiers, according to the C ++ standard (taken from clause 5.2.8 from ISO / IEC 14882: 2003):

The top-level cv qualifiers of an lvalue expression, or a type identifier that is the typeid operand, are always ignored. [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

      



-end example]

So, the result you see is expected.

+4


source







All Articles