Boost :: any type optimization for C ++ 11

Possible duplicate:
When can typeid return different instances of type_info for the same type?

If I changed the line operand->type() == typeid(ValueType)below to &operand->type() == &typeid(ValueType), the code still works with gcc and takes up less space in the executable file (and has been doing this for many years), but does the C ++ 11 standard give any guarantees that this optimization should work on different compilers?

template<typename ValueType>
ValueType * any_cast(any * operand)
{
    return operand && 
#ifdef BOOST_AUX_ANY_TYPE_ID_NAME
        std::strcmp(operand->type().name(), typeid(ValueType).name()) == 0
#else
        operand->type() == typeid(ValueType)
#endif
        ? &static_cast<any::holder<ValueType> *>(operand->content)->held
        : 0;
}
+5
source share
1 answer

No, this is not guaranteed. This statement may cause:

assert(&typeid(int) == &typeid(int));

Although a rather stupid compiler is required to create this fire, it can happen. In practice, this will fail if the typeid are compared across the dynamic boundaries of the library:

assert(&typeid_of_int_in_lib1() == &typeid_of_int_in_lib2());

It will almost certainly cause.

+5

All Articles