问题
In C++11 it is possible to determine if a variable of type A can be implicitly converted to type B by using std::is_convertible<A, B>
.
This works well if you actually know the types A and B, but all I have is type_infos. So what I'm looking for is a function like this:
bool myIsConvertible(const type_info& from, const type_info& to);
Is it possible to implement something like that in C++? If so, how?
回答1:
It is not possible in portable C++ to do what you want.
It may be possible to achieve a partial answer if you restrict yourself to a given platform. For example those platforms that adhere to the 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);
In this ABI, abi::__class_type_info
is a type derived from std::type_info
, and all std::type_info
s in the program have a dynamic type derived from std::type_info
(abi::__class_type_info
being just one example).
Using this ABI it is possible to build a tool that will navigate the inheritance hierarchy of any type (at run time), given its std::type_info
. And in doing so you could determine if two std::type_info
s represent two types that could be dynamic_cast
or even static_cast
to each other.
Note that such a solution would not take into account converting among types using a converting constructor or conversion operator. And even if that restriction is acceptable, I don't recommend this route. This is not an easy project, and would be very error prone. But this is probably how your C++ implementation implements dynamic_cast
, so it is obviously not impossible.
回答2:
I guess this could be done in case you know the typeid of your variable , Which you can always know using the typeid operator in c++ .
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 *"
Then you would have to create a multimap
or with key as typeid
(which you want to know if is convertible to ) and value as convertible type ids
( convertable type ) Where like if . Here in this case then you can access the map to search if a key
in your case const type_info& from
has a value
mapped to const type_info& to
. If Yes then you can return bool as true
or false
. But in this case you need to make sure that you see all classes and there inheritance in the code properly . And on the basis decides if it will be a legal conversion and add on the map on that basis . But this would be a tedious process and I dont see any use of it .
Genrally c++ lets you know through dynamic cast
if a type can be casted to other type or not properly. While static_cast
will even cast incompitble types to one another and Improper use of which would lead to run-time error
来源:https://stackoverflow.com/questions/10502423/stdis-convertible-for-type-info