Requiring virtual function overrides to use override keyword

后端 未结 4 1838
孤独总比滥情好
孤独总比滥情好 2020-12-29 01:47

C++11 added override to ensure that member functions you write that you intend to override base-class virtual functions actually do (or won\'t compile).

4条回答
  •  别那么骄傲
    2020-12-29 01:56

    The problem I see with -Werror=suggest-override is that it does not allow you to write the following:

    void f() final {...}
    

    Even though there is an implicit override here. The -Werror=suggest-override does not ignore this (like it should, since the override is redundant in this case)

    But it is more complicated than that... If you write

    virtual void f() final {...}
    

    It means a completely different thing than

    virtual void f() override final {...}
    

    The first case does not need to override anything! The second does.

    So I am assuming the GCC check is implemented this way (i.e. to sometimes accept the redundant override) in order to get the last case right. But this does not play well e.g. with clang-tidy, which will correctly remove the override when final is sufficient (but then GCC compilation will fail...)

提交回复
热议问题