编译器如何从C ++的新final关键字中受益? [英] How does the compiler benefit from C++'s new final keyword?

查看:96
本文介绍了编译器如何从C ++的新final关键字中受益?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 11允许将类和虚方法标记为 final ,以禁止派生或覆盖它们。

C++11 will allow to mark classes and virtual method to be final to prohibit deriving from them or overriding them.

class Driver {
  virtual void print() const;
};
class KeyboardDriver : public Driver {
  void print(int) const final;
};
class MouseDriver final : public Driver {
  void print(int) const;
};
class Data final {
  int values_;
};

这非常有用,因为它告诉界面的读者一些使用目的此类/方法。如果用户尝试覆盖,则获得诊断信息也可能很有用。

This is very useful, because it tells the reader of the interface something about the intent of the use of this class/method. That the user gets diagnostics if he tries to override might be useful, too.

但是从编译器的角度来看有优势吗?当编译器知道永远不会派生该类或永远不会覆盖此虚函数时,编译器可以做任何不同的事情吗?

But is there an advantage from the compilers point of view? Can the compiler do anything different when he knows "this class will never be derived from" or "this virtual function will never be overridden"?

对于最终我主要发现的只是N2751。在讨论的过程中,我发现参数来自C ++ / CLI,但没有明确提示为什么 final 对于编译器可能有用。我正在考虑这一点,因为我还看到了标记类 final 的一些缺点:要对受保护的成员函数进行单元测试,可以派生一个类并插入测试代码。有时,这些类是很好的候选者,需要用 final 标记。在这种情况下,这种技术是不可能的。

For final I mainly found only N2751 referring to it. Sifting through some of the discussions I found arguments coming from the C++/CLI side, but no clear hint why final may be useful for the compiler. I am thinking about this, because I also see some disadvantages of marking a class final: To unit-test protected member functions one can derive a class and insert test-code. Sometimes these classes are good candidates to be marked with final. This technique would be impossible in these cases.

推荐答案

我可以想到一种方案,该方案可能会从一个优化角度。我不确定对于编译器实现者是否值得付出努力,但至少在理论上是可行的。

I can think of one scenario where it might be helpful to the compiler from an optimisation perspective. I'm not sure if it's worth the effort to compiler implementers, but it's theoretically possible at least.

具有虚拟在派生的 final 类型上进行呼叫调度,可以确保没有其他任何派生于该类型的派生类型。这意味着(至少在理论上) final 关键字将使在编译时正确解析某些 virtual 调用成为可能。时间,这将使许多优化成为可能,而这些优化在虚拟调用中是不可能的。

With virtual call dispatch on a derived, final type you can be sure that there is nothing else deriving from that type. This means that (at least in theory) the final keyword would make it possible to correctly resolve some virtual calls at compile time, which would make a number of optimisations possible that were otherwise impossible on virtual calls.

例如,如果您有删除了most_derived_ptr ,其中 most_derived_ptr 是指向衍生的 final 类型,则编译器可以简化对 virtual 析构函数的调用。

For example, if you have delete most_derived_ptr, where most_derived_ptr is a pointer to a derived, final type then it's possible for the compiler to simplify calls to the virtual destructor.

向最派生类型的引用/指针调用 virtual 成员函数一样。

Likewise for calls to virtual member functions on references/pointers to the most derived type.

如果今天有任何编译器这样做,我会感到非常惊讶,但是似乎这种事情可能在未来十年左右的时间内实现。

I'd be very surprised if any compilers did this today, but it seems like the kind of thing that might be implemented over the next decade or so.

能够推断出(在没有 friend s的情况下)标记为<$ c在级别 类别中受$ c>保护的也实际上成为私人

There might also be some millage in being able to infer that (in the absence of friends) things marked protected in a final class also effectively become private.

这篇关于编译器如何从C ++的新final关键字中受益?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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