C ++的最终意味着在所有方面的最终? [英] Does C++ final imply final in all aspects?

查看:156
本文介绍了C ++的最终意味着在所有方面的最终?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


C ++ 11添加了最后。

C++11 added final.

最后!

我明白 final 有两件事情:


  • 一个类不可继承。

  • 在类中使(虚拟)函数不可重写(在派生类中)。

这两者似乎彼此独立。但以下列为例:

Both of these seem independent of each other. But take for example the following:

class Foo
{
    public:
    virtual void bar()
    {
        //do something unimportant.
    }
};
class Baz final : public Foo
{
    public:
    void bar() /*final*/ override
    {
        //do something more important than Foo's bar.
    }
};

从上面,我相信 Baz code>, c ,成员函数 bar 也是 final 。由于 Baz 无法继承,覆盖 bar 的问题超出范围。但是我的编译器VC ++ 2015,非常安静。我目前没有在任何其他人测试这个。

From above, I believe Baz being final, I should NOT need to specify that its virtual member function bar is also final. Since Baz cannot be inherited, the question of overriding bar goes out of scope. However my compiler VC++ 2015, is very quiet about this. I have not tested this on any others at the moment.

我很高兴,如果有人能谈谈这个话题的一些光。标准(如果有)的报价将非常感谢。

I would be glad if someone could shed some light on this topic. A quote from the standard (if any) would be extremely appreciated. Also please state any corner cases that I am unaware of, that may cause my logical belief to fail.

因此,我的问题是:是否 final class 暗示其 虚拟 final ?应该是?请澄清。

So, my question is: Does a final class implicitly imply its virtual functions to be final as well? Should it? Please clarify.

我问这是因为 final 函数符合取消虚拟化,这是一个很好的优化。

The reason I am asking this is because final functions become qualified for de-virtualization, which is a great optimization. Any help is appreciated.

推荐答案


我问这个问题的原因是因为最终的功能合格去虚拟化,这是一个很大的优化。

The reason I am asking this is because final functions become qualified for de-virtualization, which is a great optimization.

他们吗? 去虚拟化不是C ++标准的一部分。

Do they? "De-virtualization" is not part of the C++ standard. Or at least, not really.

取消虚拟化只是似乎规则的结果,它规定实现可以做任何它喜欢,只要

De-virtualization is merely a consequence of the "as if" rule, which states that the implementation can do whatever it likes so long as the implementation behaves "as if" it is doing what the standard says.

如果编译器可以在编译时检测到对虚函数成员函数的特定调用,通过多态类型,将不可否认地调用该函数的特定版本,则允许避免使用虚拟调度逻辑并静态调用函数。这就像使用虚拟分派逻辑一样,因为编译器可以证明这是被调用的函数。

If the compiler can detect at compile-time that a particular call to a virtual member function, through a polymorphic type, will undeniably call a specific version of that function, then it is allowed to avoid using the virtual dispatching logic and calling the function statically. That's behaving "as if" it had used the virtual dispatching logic, since the compiler can prove that this is the function that would have been called.

因此,标准没有定义何时允许/禁止去虚拟化。编译器在内联一个接受一个虚拟类型的指针的类时,可能会发现被传递的指针指向在函数中声明的栈变量local,它被内联。或者编译器可以将特定的内联/调用图跟踪到特定多态指针/引用的原点。在这些情况下,编译器可以将调用去虚拟化为该类型。

As such, the standard does not define when de-virtualization is allowed/forbidden. A compiler, upon inlining a class that takes a pointer to a virtual type, may find that the pointer being passed is pointing to a stack variable local declared in the function that it is being inlined within. Or that the compiler can trace down a particular inline/call graph to the point of origin for a particular polymorphic pointer/reference. In those cases, the compiler can de-virtualize calls into that type. But only if it's smart enough to do so.

编译器会将所有虚拟函数调用虚拟化为 final 类,无论这些方法是否声明 final 自己?它可能。它可能不是。它甚至可能不会对多态类型上声明为 final 的方法的任何调用。这是一个有效的(如果不是特别明亮)的实现。

Will a compiler devirtualize all virtual function calls to a final class, regardless of whether those methods are declared final themselves? It may. It may not. It may not even devirtualize any calls to methods declared final on the polymorphic type. That's a valid (if not particularly bright) implementation.

你问的问题是具体实现。

The question you're asking is implementation specific. It can vary from compiler to compiler.

但是,正如你所指出的,声明为 final 的类应该是足够的信息,使编译器将所有对指向/ 最终类类型的引用的调用去虚拟化。如果编译器不这样做,那么这是一个实现质量的问题,而不是一个标准的问题。

However, a class being declared final, as you pointed out, ought to be sufficient information for the compiler to devirtualize all calls to pointers/references to the final class type. If a compiler doesn't do so, then that's a quality-of-implementation issue, not a standards one.

这篇关于C ++的最终意味着在所有方面的最终?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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