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-orType2
is beforeType1
.// 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?