替代的虚函数调用实现? [英] Alternative virtual function calls implementations?

查看:163
本文介绍了替代的虚函数调用实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++支持通过虚拟机制进行动态绑定.但是据我了解,虚拟机制是编译器的实现细节,而该标准仅指定了在特定情况下应发生的行为.大多数编译器通过虚拟表和虚拟指针来实现虚拟机制.这与虚拟指针和表的实现细节无关.我的问题是:

C++ supports dynamic binding through virtual mechanism. But as I understand the virtual mechanism is an implementation detail of the compiler and the standard just specifies the behaviors of what should happen under specific scenarios. Most compilers implement the virtual mechanism through the virtual table and virtual pointer. This is not about implementation detail of virtual pointers and table. My questions are:

  1. 除了虚拟指针和虚拟表机制之外,是否有任何编译器以其他方式实现虚拟函数的动态分配?据我所知(通过阅读G ++,Microsoft Visual Studio),大多数人都是通过虚拟表指针机制来实现的.那么实际上还有其他编译器实现吗?
  2. 仅具有虚函数的任何类的sizeof将是该编译器上的指针大小(this中的vptr).因此,既然虚拟指针和TBL机制本身就是编译器实现,那么我上面所做的这一陈述是否总是正确的?
  1. Are there any compilers which implement dynamic dispatch of virtual functions in any other way other than the virtual pointer and virtual table mechanism? As far as I have seen most (read G++, Microsoft Visual Studio) implement it through virtual table, pointer mechanism. So practically are there any other compiler implementations at all?
  2. The sizeof of any class with just a virtual function will be size of an pointer (vptr inside this) on that compiler. So given that virtual pointer and TBL mechanism itself is compiler implementation, will this statement I made above be always true?

推荐答案

对象中的vtable指针始终是最高效的,这是不正确的.我的另一种语言的编译器出于类似的原因曾经使用对象内指针,但不再使用:而是使用单独的数据结构将对象地址映射到所需的元数据:在我的系统中,这恰好是形状信息以供使用通过垃圾收集器.

It is not true that vtable pointers in objects are always the most efficient. My compiler for another language used to use in-object pointers for similar reasons but no longer does: instead it uses a separate data structure which maps the object address to the required meta-data: in my system this happens to be shape information for use by the garbage collector.

此实现为单个简单对象花费更多的存储空间,对于具有多个基础的复杂对象更有效,并且对于数组,非常效率更高,因为在其中仅需要一个条目数组中所有对象的映射表.我的特定实现还可以找到给定指向对象内部任意点的指针的元数据.

This implementation costs a bit more storage for a single simple object, is more efficient for complex objects with many bases, and it is vastly more efficient for arrays, since only a single entry is required in the mapping table for all objects in the array. My particular implementation can also find the meta-data given a pointer to any point interior to the object.

由于我使用的是地球上最好的数据结构:Judy阵列,因此实际查找速度非常快,并且存储要求也非常适中.

The actual lookup is extremely fast, and the storage requirements very modest, because I am using the best data structure on the planet: Judy arrays.

我也知道没有C ++编译器使用除vtable指针以外的任何东西,但这不是唯一的方法.实际上,具有基类的初始化语义会使任何实现变得混乱.这是因为在构造对象时,必须完整地查看类型.这些语义的结果是,复杂的混合对象导致生成大量vtable,大型对象和缓慢的对象初始化.这可能不是vtable技术的结果,而是需要严格遵循子对象的运行时类型始终正确的要求.实际上,在构造过程中没有充分的理由,因为构造函数不是方法,并且不能明智地使用虚拟调度:由于析构函数是真实的方法,因此对于销毁我来说,这不是很清楚.

I also know of no C++ compiler using anything other than vtable pointers, but it is not the only way. In fact, the initialisation semantics for classes with bases make any implementation messy. This is because the complete type has to see-saw around as the object is constructed. As a consequence of these semantics, complex mixin objects lead to massive sets of vtables being generated, large objects, and slow object initialisation. This probably isn't a consequence of the vtable technique as much as needing to slavishly follow the requirement that the run-time type of a subobject be correct at all times. Actually there's no good reason for this during construction, since constructors are not methods and can't sensibly use virtual dispatch: this isn't so clear to me for destruction since destructors are real methods.

这篇关于替代的虚函数调用实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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