typeid运算符 [英] typeid operator

查看:95
本文介绍了typeid运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


我想使用typeid返回的引用作为std :: map中的键。

假设是安全的那个typeid(T)(其中T是一个类型名称)将始终

返回对给定T的相同type_info结构的相同引用?我的

地图看起来像这样:

typedef std :: map< const std :: type_info&,......> Type_map;


干杯,

Marco

解决方案

" Marco Jez" ; <是ne ***** @ nessuno.com>在留言中写道

新闻:FO ********************** @ news4.tin.it ...
< blockquote class =post_quotes>我想使用typeid返回的引用作为std :: map中的键。
假设typeid(T)(其中T是类型名称)总是安全的
对给定的T返回相同的type_info结构的相同引用?




我不明白为什么你应该能够做出这个假设。


type_info :: before成员用于定义一个

比较函数,可用于从typeids中生成地图键。如果您使用它,则表示相同类型的两个不同的type_info对象

应被视为等效键。


< blockquote> Marco Jez写道:

我想使用typeid返回的引用作为std :: map中的键。
是否可以安全地假设typeid(T)(其中T是一个类型名称)将始终
返回给定T的相同type_info结构的相同引用?我的
地图看起来像这样:

typedef std :: map< const std :: type_info&,......> Type_map;




是的,这是有保证的。


V

Andrew Koenig写道:

" Marco Jez" <是ne ***** @ nessuno.com>在消息中写道
新闻:FO ********************** @ news4.tin.it ...

< blockquote class =post_quotes>我想使用typeid返回的引用作为std :: map中的键。
假设typeid(T)(其中T是类型名称)总是安全的
对给定的T返回相同的type_info结构的相同引用?

我不明白为什么你应该能够做出这个假设。




我认为5.2.8中的标准表示返回左值并且左值所引用的对象的生命周期是整个程序。


那么,为什么我们能够做出这样的假设呢?或者你忘记了

''不'',因为我不明白为什么你应该_不能......&#;


我唯一怀疑的是,OP的地图是作为关键类型引用

。可能吗? "钥匙及QUOT;需要是可分配的。

引用是否可分配?我觉得它们很好,但是它们是什么?

type_info :: before成员用于定义一个
比较函数,你可以用它来制作地图键。 typeids。如果您使用它,则表示相同类型的两个不同的type_info对象应被视为等效键。




V

Hi everyone!

I would like to use the reference returned by typeid as key in a std::map.
Is it safe to assume that typeid(T) (where T is a type name) will always
return the same reference to the same type_info structure for a given T? My
map would look like this:

typedef std::map<const std::type_info &, ......> Type_map;

Cheers,
Marco

解决方案

"Marco Jez" <ne*****@nessuno.com> wrote in message
news:FO**********************@news4.tin.it...

I would like to use the reference returned by typeid as key in a std::map.
Is it safe to assume that typeid(T) (where T is a type name) will always
return the same reference to the same type_info structure for a given T?



I don''t see why you should be able to make that assumption.

The type_info::before member is intended to be used for defining a
comparison function that you can use for making map keys out of typeids. If
you use it, two distinct type_info objects that represent the same type
should be treated as equivalent keys.


Marco Jez wrote:

I would like to use the reference returned by typeid as key in a std::map.
Is it safe to assume that typeid(T) (where T is a type name) will always
return the same reference to the same type_info structure for a given T? My
map would look like this:

typedef std::map<const std::type_info &, ......> Type_map;



Yes, that''s guaranteed.

V


Andrew Koenig wrote:

"Marco Jez" <ne*****@nessuno.com> wrote in message
news:FO**********************@news4.tin.it...

I would like to use the reference returned by typeid as key in a std::map.
Is it safe to assume that typeid(T) (where T is a type name) will always
return the same reference to the same type_info structure for a given T?

I don''t see why you should be able to make that assumption.



I think the Standard in 5.2.8 says that the lvalue is returned and that
the lifetime of the object referred to by the lvalue is entire program.
So, why should we be able to make that assumption? Or did you forget the
''not'' as in "I don''t see why you should _not_ be able..."?

The only doubt I have is that the OP''s map is made to have the reference
as the key type. Is it possible? "Key" is required to be assignable.
Are references assignable? I kind of think they are fine, but are they?
The type_info::before member is intended to be used for defining a
comparison function that you can use for making map keys out of typeids. If
you use it, two distinct type_info objects that represent the same type
should be treated as equivalent keys.



V


这篇关于typeid运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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