C++: type_info to distinguish types

前端 未结 5 1965
太阳男子
太阳男子 2021-02-01 20:20

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 l

5条回答
  •  星月不相逢
    2021-02-01 20:53

    Type_info is implementation defined so I really wouldn't rely on it. However, based on my experiences using g++ and MSVC, assumptions 1,3 and 4 hold... not really sure about #2.

    Is there any reason you can't use another method like this?

    template
    struct is_same       { static bool const result = false; };
    
    template
    struct is_same { static bool const result = true;  };
    
    template
    bool IsSame(const S& s, const T& t) {   return is_same::result; }
    

提交回复
热议问题