c++rttitypeidtypeinfo

Which platforms don't use string comparison in type_info op==?


Here is a typical implementation of type_info::operator==:

#if _PLATFORM_SUPPORTS_UNIQUE_TYPEINFO
    bool operator==(const type_info& __rhs) const {
      return __mangled_name == __rhs.__mangled_name;
    }
#else
    bool operator==(const type_info& __rhs) const {
      return __mangled_name == __rhs.__mangled_name ||
             strcmp(__mangled_name, __rhs.__mangled_name) == 0;
    }
#endif

In libstdc++ it's controlled with __GXX_MERGED_TYPEINFO_NAMES,
in libc++ it's _LIBCPP_NONUNIQUE_RTTI_BIT,
MSVC always compares strings.

What are the platforms which don't compare strings?


Solution

  • In libstdc++ it's controlled with __GXX_MERGED_TYPEINFO_NAMES

    In newer versions of gcc (since 23 Jul 2009) this macro is set to 0 by default. It always compares pointers first and if that fails they do the full string comparison. See here:

    We used to do inline pointer comparison by default if weak symbols are available, but even with weak symbols sometimes names are not merged when objects are loaded with RTLD_LOCAL, so now we always use strcmp by default.