C ++中的动态与静态多态性:哪个更可取?

我了解动态/静态多态性取决于应用程序设计和要求.但是,如果可能的话,是否总是选择静态多态性而不是动态多态性?特别是,我可以在我的应用程序中看到以下两种设计选择,这两种选择似乎都被建议不要使用:

I understand that dynamic/static polymorphism depends on the application design and requirements. However, is it advisable to ALWAYS choose static polymorphism over dynamic if possible? In particular, I can see the following 2 design choice in my application, both of which seem to be advised against:

  1. 使用 CRTP 实现静态多态性:没有 vtable 查找开销,同时仍以模板基类的形式提供接口.但是,使用大量的 switch 和 static_cast 来访问正确的类/方法,这是危险的

  1. Implement Static polymorphism using CRTP: No vtable lookup overhead while still providing an interface in form of template base class. But, uses a Lot of switch and static_cast to access the correct class/method, which is hazardous

动态多态性:实现接口(纯虚拟类),为访问器/突变器等微不足道的函数关联查找成本

Dynamic Polymorphism: Implement interfaces (pure virtual classes), associating lookup cost for even trivial functions like accessors/mutators

我的应用程序对时间非常关键,所以我赞成静态多态性.但是需要知道使用过多的 static_cast 是否表明设计不佳,以及如何在不产生延迟的情况下避免这种情况.

My application is very time critical, so am in favor of static polymorphism. But need to know if using too many static_cast is an indication of poor design, and how to avoid that without incurring latency.

感谢您的洞察力.举一个具体的案例,哪种方法更好?

Thanks for the insight. Taking a specific case, which of these is a better approach?

class IMessage_Type_1
{
  virtual long getQuantity() =0;
...
}
class Message_Type_1_Impl: public IMessage_Type_1
{
  long getQuantity() { return _qty;}
...
}

template <class T>
class TMessage_Type_1
{
  long getQuantity() { return static_cast<T*>(this)->getQuantity(); }
...
}
class Message_Type_1_Impl: public TMessage_Type_1<Message_Type_1_Impl>
{
  long getQuantity() { return _qty; }
...
}

请注意,每个类中有几个修改器/访问器,我确实需要在我的应用程序中指定一个接口.在静态多态中,我只切换一次――获取消息类型.但是,在动态多态性中,我使用虚函数进行每个方法调用.这不是使用静态多边形的理由吗?我相信 CRTP 中的 static_cast 非常安全并且没有性能损失(编译时间限制)?

Note that there are several mutators/accessors in each class, and I do need to specify an interface in my application. In static polymorphism, I switch just once - to get the message type. However, in dynamic polymorphism, I am using virtual functions for EACH method call. Doesnt that make it a case to use static poly? I believe static_cast in CRTP is quite safe and no performance penalty (compile time bound) ?

推荐答案

一个 switch 只不过是一个跳转序列,经过优化后,会跳转到一个通过表查找的地址.和虚函数调用一模一样.

A switch is nothing more than a sequence of jumps that -after optimized- becomes a jump to an address looked-up by a table. Exactly like a virtual function call is.

如果必须根据类型进行跳转,则必须先选择类型.如果无法在编译时进行选择(主要是因为它取决于输入),则必须始终执行两个操作:select &跳.您用于选择的语法工具不会改变性能,因为优化相同.

If you have to jump depending on a type, you must first select the type. If the selection cannot be done at compile time (essentially because it depends on the input) you must always perform two operation: select & jump. The syntactic tool you use to select doesn't change the performance, since optimize the same.

事实上,您正在重新发明 v-table.

In fact you are reinventing the v-table.

相关文章