C ++:type_info来区分类型 [英] C++: type_info to distinguish types
问题描述
我知道编译器在实现 std :: type_info
函数行为方面有很大的自由。
考虑使用它来比较对象类型,所以我想确定:
-
std :: type_info :: name
必须为两种不同类型返回两个不同的字符串。 -
std :: type_info :: before
必须说Type1
之前是Type2
exclusive-or之前
Type1
//像这样:
typeid(T1).before(typeid(T2))!= typeid T2).before(typeid(T1))
-
-
两个不同的
typedef
- 相同类型的类型是相同类型。
最后:
-
由于
std :: type_info
不可复制,我如何存储type_info
在std :: map
)?有一个std :: type_info
总是分配到某个地方(例如:在堆栈或一个静态/全局变量),并使用指针的唯一方法? -
运算符==
,运算符的速度有多快?=
和之前
在最常见的编译器?我想他们应该只比较一个值。typeid
? -
我有一个类
A
与virtual bool operator ==(const A&)const
。由于A
有很多子类(其中一些在编译时是未知的),我将重载任何子类B
这样:virtual bool operator ==(const A& other)const {
if (typeid(* this)!= typeid(other))return false;
// bool B :: operator ==(const B& other)const //为任何类B定义
return operator ==(static_cast< B&>(other));这是一个可以接受的(和标准的)实现这样的操作符的方式吗? p>
快速浏览文档后, p>
-
std :: type_info :: name总是返回两个不同类型的两个不同的字符串,否则意味着编译器在解析时丢失
-
引用告诉:如果类型先于排序规则中的rhs类型,则返回true。命令只是一个由特定实现保存的内部命令,不一定与继承关系或声明命令有关。
因此,您可以确保没有类型在排序顺序中具有相同的排名。 -
模板类的每个实例化都是不同的类型。专业化也不例外。
-
我真的不明白你的意思。如果你的意思是像在两个单独的编译单元中有
typedef foo bar;
,并且这两个栏是相同的,它的工作原理。如果你的意思是typedef foo bar;
- 您应该存储对std :: type_info的引用,以某种方式进行换行。
- 绝对不知道性能,我假设比较运算符有固定的时间,尽管类型复杂。根据代码中使用的不同类型的数量,之前必须有线性复杂性。
- 这真的很奇怪。您应该重载
运算符==
,而不是将其设为虚拟并覆盖它。
I know that compilers have much freedom in implementing std::type_info
functions' behavior.
I'm thinking about using it to compare object types, so I'd like to be sure that:
std::type_info::name
must return two different strings for two different types.std::type_info::before
must say thatType1
is beforeType2
exclusive-orType1
is beforeType2
.// like this: typeid(T1).before( typeid(T2) ) != typeid(T2).before( typeid(T1) )
Two different specialization of the same template class are considered different types.
Two different
typedef
-initions of the same type are the same type.
And finally:
Since
std::type_info
is not copyable, how could I storetype_info
s somewhere (eg: in astd::map
)? The only way it to have astd::type_info
always allocated somewhere (eg: on the stack or on a static/global variable) and use a pointer to it?How fast are
operator==
,operator!=
andbefore
on most common compilers? I guess they should only compare a value. And how fast istypeid
?I've got a class
A
with avirtual bool operator==( const A& ) const
. SinceA
has got many subclasses (some of which are unknown at compile time), I'd overload that virtual operator in any subclassB
this way:virtual bool operator==( const A &other ) const { if( typeid(*this) != typeid(other) ) return false; // bool B::operator==( const B &other ) const // is defined for any class B return operator==( static_cast<B&>( other ) ); }
Is this an acceptable (and standard) way to implement such operator?
After a quick look at the documentation, I would say that :
std::type_info::name always returns two different strings for two different types, otherwise it means that the compiler lost itself while resolving types and you shouldn't use it anymore.
Reference tells : "before returns true if the type precedes the type of rhs in the collation order. The collation order is just an internal order kept by a particular implementation and is not necessarily related to inheritance relations or declaring order." You therefore have the guarantee that no types has the same rank in the collation order.
Each instantiation of a template class is a different type. Specialization make no exceptions.
I don't really understand what you mean. If you mean something like having
typedef foo bar;
in two separate compilation units and that bar is the same in both, it works that way. If you meantypedef foo bar; typedef int bar;
, it doesn't work (except if foo is int).
About your other questions :
- You should store references to std::type_info, of wrap it somehow.
- Absolutely no idea about performance, I assume that comparison operators have constant time despite of the type complexity. Before must have linear complexity depending on the number of different types used in your code.
- This is really strange imho. You should overload your
operator==
instead of make it virtual and override it.
这篇关于C ++:type_info来区分类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!