c++typeidtypeinfo

C++: type_info to distinguish types


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:

  1. std::type_info::name must return two different strings for two different types.

  2. std::type_info::before must say that Type1 is before Type2 exclusive-or Type2 is before Type1.

    // like this:
    typeid(T1).before( typeid(T2) ) != typeid(T2).before( typeid(T1) )
    
  3. Two different specialization of the same template class are considered different types.

  4. Two different typedef-initions of the same type are the same type.

And finally:


Solution

  • After a quick look at the documentation, I would say that :

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

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

    3. Each instantiation of a template class is a different type. Specialization make no exceptions.

    4. 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 mean typedef foo bar; typedef int bar;, it doesn't work (except if foo is int).

    About your other questions :