typeid - C++: type_info to distinguish types -
i know compilers have freedom in implementing std::type_info functions' behavior.
i'm thinking using compare object types, i'd sure that:
std::type_info::namemust return 2 different strings 2 different types.std::type_info::beforemusttype1beforetype2exclusive-ortype1beforetype2.// this: typeid(t1).before( typeid(t2) ) != typeid(t2).before( typeid(t1) )two different specialization of same template class considered different types.
two different
typedef-initions of same type same type.
and finally:
since
std::type_infonot copyable, how storetype_infos somewhere (eg: instd::map)? way havestd::type_infoallocated somewhere (eg: on stack or on static/global variable) , use pointer it?how fast
operator==,operator!=,beforeon common compilers? guess should compare value. , how fasttypeid?i've got class
avirtual bool operator==( const a& ) const. sinceahas got many subclasses (some of unknown @ compile time), i'd overload virtual operator in subclassbway:virtual bool operator==( const &other ) const { if( typeid(*this) != typeid(other) ) return false; // bool b::operator==( const b &other ) const // defined class b return operator==( static_cast<b&>( other ) ); }is acceptable (and standard) way implement such operator?
after quick @ documentation, :
std::type_info::name returns 2 different strings 2 different types, otherwise means compiler lost while resolving types , shouldn't use anymore.
reference tells : "before returns true if type precedes type of rhs in collation order. collation order internal order kept particular implementation , not related inheritance relations or declaring order." therefore have guarantee no types has same rank in collation order.
each instantiation of template class different type. specialization make no exceptions.
i don't understand mean. if mean having
typedef foo bar;in 2 separate compilation units , bar same in both, works way. if meantypedef foo bar; typedef int bar;, doesn't work (except if foo int).
about other questions :
- you should store references std::type_info, of wrap somehow.
- absolutely no idea performance, assume comparison operators have constant time despite of type complexity. before must have linear complexity depending on number of different types used in code.
- this strange imho. should overload
operator==instead of make virtual , override it.
Comments
Post a Comment