typeid运算符

扫码查看
我认为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 alwaysreturn the same reference to the same type_info structure for a given T? Mymap would look like this:typedef std::map<const std::type_info &, ......> Type_map;Cheers,Marco 解决方案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 acomparison function that you can use for making map keys out of typeids. Ifyou use it, two distinct type_info objects that represent the same typeshould be treated as equivalent keys.Yes, that''s guaranteed.VI think the Standard in 5.2.8 says that the lvalue is returned and thatthe 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 referenceas 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运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-19 16:18
查看更多