C ++:type_info来区分类型 [英] C++: type_info to distinguish types

查看:139
本文介绍了C ++:type_info来区分类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道编译器在实现 std :: type_info 函数行为方面有很大的自由。



考虑使用它来比较对象类型,所以我想确定:


  1. std :: type_info :: name 必须为两种不同类型返回两个不同的字符串。


  2. std :: type_info :: before 必须说 Type1 之前是 Type2 exclusive-or 之前 Type1

      //像这样:
    typeid(T1).before(typeid(T2))!= typeid T2).before(typeid(T1))



  3. 两个不同的 typedef - 相同类型的类型是相同类型。


最后:




  • 由于 std :: type_info 不可复制,我如何存储 type_info std :: map )?有一个 std :: type_info 总是分配到某个地方(例如:在堆栈或一个静态/全局变量),并使用指针的唯一方法?


  • 运算符== 运算符的速度有多快?= 之前在最常见的编译器?我想他们应该只比较一个值。 typeid


  • 我有一个类 A virtual bool operator ==(const A&)const 。由于 A 有很多子类(其中一些在编译时是未知的),我将重载任何子类 B 这样:

      virtual bool operator ==(const A& other)const {
    if (typeid(* this)!= typeid(other))return false;
    // bool B :: operator ==(const B& other)const //为任何类B定义
    return operator ==(static_cast< B&>(other));这是一个可以接受的(和标准的)实现这样的操作符的方式吗? p>


解决方案

快速浏览文档后, p>


  1. std :: type_info :: name总是返回两个不同类型的两个不同的字符串,否则意味着编译器在解析时丢失


  2. 引用告诉:如果类型先于排序规则中的rhs类型,则返回true。命令只是一个由特定实现保存的内部命令,不一定与继承关系或声明命令有关。
    因此,您可以确保没有类型在排序顺序中具有相同的排名。


  3. 模板类的每个实例化都是不同的类型。专业化也不例外。


  4. 我真的不明白你的意思。如果你的意思是像在两个单独的编译单元中有 typedef foo bar; ,并且这两个栏是相同的,它的工作原理。如果你的意思是 typedef foo bar;




关于您的其他问题:




  • 您应该存储对std :: type_info的引用,以某种方式进行换行。

  • 绝对不知道性能,我假设比较运算符有固定的时间,尽管类型复杂。根据代码中使用的不同类型的数量,之前必须有线性复杂性。

  • 这真的很奇怪。您应该重载运算符== ,而不是将其设为虚拟并覆盖它。


I know that compilers have much freedom in implementing std::type_info functions' behavior.

I'm thinking about using it to compare object types, so I'd like to be sure that:

  1. std::type_info::name must return two different strings for two different types.

  2. std::type_info::before must say that Type1 is before Type2 exclusive-or Type1 is before Type2.

    // like this:
    typeid(T1).before( typeid(T2) ) != typeid(T2).before( typeid(T1) )
    

  3. Two different specialization of the same template class are considered different types.

  4. Two different typedef-initions of the same type are the same type.

And finally:

  • Since std::type_info is not copyable, how could I store type_infos somewhere (eg: in a std::map)? The only way it to have a std::type_info always allocated somewhere (eg: on the stack or on a static/global variable) and use a pointer to it?

  • How fast are operator==, operator!= and before on most common compilers? I guess they should only compare a value. And how fast is typeid?

  • I've got a class A with a virtual bool operator==( const A& ) const. Since A has got many subclasses (some of which are unknown at compile time), I'd overload that virtual operator in any subclass B this way:

    virtual bool operator==( const A &other ) const {
      if( typeid(*this) != typeid(other) ) return false;
      // bool B::operator==( const B &other ) const // is defined for any class B
      return operator==( static_cast<B&>( other ) );
    }
    

    Is this an acceptable (and standard) way to implement such operator?

解决方案

After a quick look at the documentation, I would say that :

  1. std::type_info::name always returns two different strings for two different types, otherwise it means that the compiler lost itself while resolving types and you shouldn't use it anymore.

  2. Reference tells : "before returns true if the type precedes the type of rhs in the collation order. The collation order is just an internal order kept by a particular implementation and is not necessarily related to inheritance relations or declaring order." You therefore have the guarantee that no types has the same rank in the collation order.

  3. Each instantiation of a template class is a different type. Specialization make no exceptions.

  4. I don't really understand what you mean. If you mean something like having typedef foo bar; in two separate compilation units and that bar is the same in both, it works that way. If you mean typedef foo bar; typedef int bar;, it doesn't work (except if foo is int).

About your other questions :

  • You should store references to std::type_info, of wrap it somehow.
  • Absolutely no idea about performance, I assume that comparison operators have constant time despite of the type complexity. Before must have linear complexity depending on the number of different types used in your code.
  • This is really strange imho. You should overload your operator== instead of make it virtual and override it.

这篇关于C ++:type_info来区分类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆