type_info不考虑cv限定符:是这样吗? [英] type_info doesn't account for cv qualifiers: is this right?
问题描述
这是正确的行为还是这个代码打印1的g ++ 4.5的怪癖?
Is it the correct behaviour or is it a quirk of g++4.5 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));
}
我认为cv限定符的不同类型被视为非常不同的类型,即使更少的cv限定类型可以隐式转换为更多cv限定类型。
I thought that types differing for cv-qualifiers were threated as very distinct types, even though less cv-qualified types could be implicitly cast to more cv-qualified types.
推荐答案
typeid
根据C ++标准忽略cv限定符(取自ISO / IEC 14882:2003的§5.2.8):
typeid
ignores cv qualifiers, as per the C++ standard (taken from §5.2.8 from ISO/IEC 14882:2003) :
左值表达式的顶级cv限定符或者是typeid的操作数的type-id总是被忽略的
。 [示例:
The top-level cv-qualifiers of the lvalue expression or the type-id that is the operand of typeid 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
$ b
—end example]
所以,你看到的结果是预期的。
So, the result you're seeing is expected.
这篇关于type_info不考虑cv限定符:是这样吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!