typeid没有返回正确的类型 [英] typeid doesn't return correct type

查看:97
本文介绍了typeid没有返回正确的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 a type_info object representing the type of the type-id. If the type of the type-id is a reference type, the result of the typeid expression refers to a type_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屋!

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