Std :: is_convertible for type_info

In C ++ 11, you can determine whether a variable of type A can be implicitly converted to type B through using std::is_convertible<A, B>.

This works well if you really know types A and B, but all I have is type_infos. So, I am looking for a function like this:

bool myIsConvertible(const type_info& from, const type_info& to);

Is it possible to implement something similar in C ++? If so, how?

+5
source share
2 answers

In portable C ++, it's impossible to do what you want.

It may be possible to get a partial answer if you limit yourself to this platform. For example, those platforms that adhere to Itanium ABI will have an implementation of this function:

extern "C" 
void* __dynamic_cast(const void *sub,
                     const abi::__class_type_info *src,
                     const abi::__class_type_info *dst,
                     std::ptrdiff_t src2dst_offset);

ABI abi::__class_type_info - , std::type_info, std::type_info , std::type_info (abi::__class_type_info, ).

ABI, , ( ), std::type_info. , std::type_info , dynamic_cast static_cast .

, . , . , . , , , ++ dynamic_cast, , , .

+4

, , , typeid ++.

  Derived* pd = new Derived;
  Base* pb = pd;
  cout << typeid( pb ).name() << endl;   //prints "class Base *"
  cout << typeid( *pb ).name() << endl;   //prints "class Derived"
  cout << typeid( pd ).name() << endl;   //prints "class Derived *"

multimap typeid ( , ), convertible type ids ( ). , key const type_info& from value, const type_info& to. "", bool true false. , . , . , .

Genrally ++ dynamic cast, . static_cast ,

+1

All Articles