编译时间与运行时多态性在C ++中的优势/劣势 [英] Compile time vs run time polymorphism in C++ advantages/disadvantages

查看:156
本文介绍了编译时间与运行时多态性在C ++中的优势/劣势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中时,它可以使用两种运行时间(子类,虚函数)来实现相同的功能或编译时(模板函数重载)多态​​性,你为什么会选择一个比其他?

我倒觉得编译code将是编译时多态性更大(更多方法/模板类型创建的类定义),而且编译的时候会给你更多的灵活性,同时运行时会给你更安全多态(即难以被正确意外使用)。

是我的假设是否正确?是否还有其他的优势/劣势要么?谁能给出一个具体的例子,其中两个是可行的选择,但一个或另一个将是一个明显更好的选择?

此外,没有编译时多态性产生更快的code,因为它是没有必要通过虚表调用函数,或者这是否获得由编译器优化,反正走?

例如:

 类基地
{
  虚拟无效的print()= 0;
}类Derived1:基地
{
  虚拟无效的print()
  {
     //做不同的事情
  }
}类Derived2的:基地
{
  虚拟无效的print()
  {
    //做不同的事情
  }
}//运行时间
无效打印(基本O)
{
  o.print();
}//编译时间
模板< typename的T>
打印(T O)
{
  o.print();
}


解决方案

静态多态性产生,主要是因为积极的内联的可能性较快code。虚函数很少能够被内联,而且大多处于非多态性的情景。请参阅本项 C ++ FAQ 。如果速度是你的目标,你基本上costhave别无选择。

在另一方面,不仅编译时间,而且还使用静态多态,当code的可读性和可调试性要差很多。比如:抽象方法是强制实施的某个接口方法的清洁方式。要使用静态多态性达到同样的目标,你需要恢复到的概念检查或好奇地重复模板模式

当你真的要使用动态多态的唯一情况,就是当实现不可在编译时。举例来说,当它从动态库加载。在实践中你,你可能要清洁code和更快的编译交换性能。

In C++ when it is possible to implement the same functionality using either run time (sub classes, virtual functions) or compile time (templates, function overloading) polymorphism, why would you choose one over the other?

I would think that the compiled code would be larger for compile time polymorphism (more method/class definitions created for template types), and that compile time would give you more flexibility, while run time would give you "safer" polymorphism (i.e. harder to be used incorrectly by accident).

Are my assumptions correct? Are there any other advantages/disadvantages to either? Can anyone give a specific example where both would be viable options but one or the other would be a clearly better choice?

Also, does compile time polymorphism produce faster code, since it is not necessary to call functions through vtable, or does this get optimized away by the compiler anyway?

Example:

class Base
{
  virtual void print() = 0;
}

class Derived1 : Base
{
  virtual void print()
  {
     //do something different
  }
}

class Derived2 : Base
{
  virtual void print()
  {
    //do something different
  }
}

//Run time
void print(Base o)
{
  o.print();
}

//Compile time
template<typename T>
print(T o)
{
  o.print();
}

解决方案

Static polymorphism produces faster code, mostly because of the possibility of aggressive inlining. Virtual functions can rarely be inlined, and mostly in a "non-polymorphic" scenarios. See this item in C++ FAQ. If speed is your goal, you basically costhave no choice.

On the other hand, not only compile times, but also the readability and debuggability of the code is much worse when using static polymorphism. For instance: abstract methods are a clean way of enforcing implementation of certain interface methods. To achieve the same goal using static polymorphism, you need to restore to concept checking or curiously recurring template pattern.

The only situation when you really have to use dynamic polymorphism, is when the implementation is not available at compile time. For instance when it's loaded from dynamic library. In practice thou, you may want to exchange performance for cleaner code and faster compilation.

这篇关于编译时间与运行时多态性在C ++中的优势/劣势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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