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::beforemust- type1before- type2exclusive-or- type1before- type2.- // 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 store- type_infos somewhere (eg: in- std::map)? way have- std::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 fast- typeid?
- i've got class - a- virtual bool operator==( const a& ) const. since- ahas got many subclasses (some of unknown @ compile time), i'd overload virtual operator in subclass- bway:- 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 mean- typedef 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