Requiring virtual function overrides to use override keyword

后端 未结 4 1836
孤独总比滥情好
孤独总比滥情好 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:54

    Looks like the GCC 5.1 release added exactly the warning I was looking for:

    -Wsuggest-override
           Warn about overriding virtual functions that are not marked with the override keyword.

    Compiling with -Wsuggest-override -Werror=suggest-override would then enforce that all overrides use override.

    0 讨论(0)
  • 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...)

    0 讨论(0)
  • 2020-12-29 02:08

    There are two things you can do.

    First, Clang 3.5 and higher have a -Winconsistent-missing-override warning (triggered by -Wall). This does not quite work for your example, but only if you would add a void foo() override {} to class B and not in class C. What you actually want is -Wmissing-override, to locate all missing override, not just the inconsistently missing ones. That is currently not provided, but you might complain on the Clang mailinglist and they might add it.

    Second, you use Howard Hinnant's trick to temporarily add final to the base class member function and recompile. The compiler will then locate all further derived classes that attempt to override the virtual base member function. You can then fix the missing ones. It's a bit more work and requires frequent rechecking when your class hierarchy expands.

    0 讨论(0)
  • 2020-12-29 02:08

    GCC and Clang are covered by other answers. Here's same for VC++ from my other answer:

    Below are the relevant warning numbers in VC++:

    C4263 (level 4) 'function': member function does not override any base class virtual member function
    C4266 (level 4) 'function': no override available for virtual member function from base 'type'; function is hidden
    

    To enable these two warnings, you can use one of following options:

    1. Set warning level to 4 in project settings and then disable the warnings you don't want. This is my prefered way. To disable unwanted Level 4 warnings, go to project settings > C/C++ > Advanced and then enter warning numbers in Disable Specific Warnings box.
    2. Enable above two warnings using code.

      #pragma warning(default:4263)
      #pragma warning(default:4266)
      
    3. Enable above two warnings in project settings > C/C++ > Command Line and then enter /w34263 /w34266. Here /wNxxxx option means enable xxxx warnings in Level N (N = 3 is default level). You can also do /wdNxxxx which disables the xxxx warning in level N.

    0 讨论(0)
提交回复
热议问题