简体   繁体   English

type_info的std :: is_convertible

[英]std::is_convertible for type_info

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> . 在C ++ 11中,可以通过using std::is_convertible<A, B>来确定类型A的变量是否可以隐式转换为类型B.

This works well if you actually know the types A and B, but all I have is type_infos. 如果你真的知道类型A和B,这很有效,但我只有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++? 是否有可能在C ++中实现类似的东西? If so, how? 如果是这样,怎么样?

It is not possible in portable C++ to do what you want. 便携式C ++中不可能做你想做的事。

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: 例如,那些遵循Itanium ABI的平台将具有此功能的实现:

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). 在此ABI中, abi::__class_type_info是从std::type_info派生的类型,程序中的所有 std::type_info都具有从std::type_info派生的动态类型( abi::__class_type_info只是一个示例)。

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 . 使用此ABI,可以构建一个工具,该工具将在给定其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. 在这样做的过程中,您可以确定两个std::type_info代表两种类型,可以是dynamic_cast ,甚至是static_cast

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. 但这可能是你的C ++实现如何实现dynamic_cast ,所以这显然不是不可能的。

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++ . 我想这可以在你知道变量的typeid的情况下完成,你总是可以在c ++中使用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 *"

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 . 然后你必须创建一个multimap或使用key作为typeid (你想知道它是否可以转换为)和value as convertible type ids (convertable type)where if like。 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 . 在这里,在这种情况下,你可以访问地图搜索,如果一个key你的情况const type_info& from具有value映射到const type_info& to If Yes then you can return bool as true or false . 如果是,则可以返回bool为truefalse 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. 如果一个类型可以被转换为其他类型或者不正确,那么通过c ++可以让你通过dynamic cast知道。 While static_cast will even cast incompitble types to one another and Improper use of which would lead to run-time error 虽然static_cast甚至会将不可比的类型相互转换,但不正确的使用会导致运行时错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM