typeid没有返回正确的类型 [英] typeid doesn't return correct type
问题描述
cout << typeid(int&).name();
我认为
应该以类型而不是int
的形式返回int&
,但是在GCC 4.5.1和VS2010 SP1 beta上,它会返回int
.为什么会这样?
This, in my opinion, should return int&
as a type, not an int
, but on GCC 4.5.1 and on VS2010 SP1 beta it returns int
. Why is this?
推荐答案
这应该是typeid的工作方式.当将typeid
应用于引用类型的 type-id 时,type_info
对象引用引用的类型.
This is how typeid is supposed to work. When you apply typeid
to a type-id of a reference type, the type_info
object refers to the referenced type.
ISO/IEC 14882:2003,5.2.8/4 [expr.typeid]:
ISO/IEC 14882:2003, 5.2.8 / 4 [expr.typeid]:
将
typeid
应用于 type-id 时,结果引用表示 type-id 类型的type_info
对象.如果 type-id 的类型是引用类型,则typeid
表达式的结果引用表示所引用类型的type_info
对象.如果 type-id 的类型是类类型或对类类型的引用,则应完全定义该类.类型不得在 type-id 中定义.
When
typeid
is applied to a type-id, the result refers to atype_info
object representing the type of the type-id. If the type of the type-id is a reference type, the result of thetypeid
expression refers to atype_info
object representing the 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. Types shall not be defined in the type-id.
这篇关于typeid没有返回正确的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!