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:

  1. std::type_info::name must return 2 different strings 2 different types.

  2. std::type_info::before must type1 before type2 exclusive-or type1 before type2.

    // this: typeid(t1).before( typeid(t2) ) != typeid(t2).before( typeid(t1) ) 
  3. two different specialization of same template class considered different types.

  4. two different typedef-initions of same type same type.

and finally:

  • since std::type_info not copyable, how store type_infos somewhere (eg: in std::map)? way have std::type_info allocated somewhere (eg: on stack or on static/global variable) , use pointer it?

  • how fast operator==, operator!= , before on common compilers? guess should compare value. , how fast typeid?

  • i've got class a virtual bool operator==( const a& ) const. since a has got many subclasses (some of unknown @ compile time), i'd overload virtual operator in subclass b way:

    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, :

  1. std::type_info::name returns 2 different strings 2 different types, otherwise means compiler lost while resolving types , shouldn't use anymore.

  2. 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.

  3. each instantiation of template class different type. specialization make no exceptions.

  4. 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

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -