How to enforce the 'override' keyword?
Asked Answered
C

2

64

Is there any way to enforce the usage of the C++11 override keyword in Visual C++ 2012?

(i.e. if I forget to say override, then I want to get a warning/error.)

Clash answered 4/11, 2012 at 21:46 Comment(2)
To answer your question concretely, no, VC++ 2012 RTM contains no such warning (even disabled by default).Trefoil
@ildjarn: Thanks, that's the kind of answer I was looking for. :)Clash
S
22

C++11 almost had what you want.

Originally the override keyword was part of a larger proposal (N2928) which also included the ability to enforce its usage:

class A
{
  virtual void f();
};

class B [[base_check]] : public A
{
    void f();  // error!
};

class C [[base_check]] : public A
{
  void f [[override]] ();  // OK
};

The base_check attribute would make it an error to override a virtual function without using the override keyword.

There was also a hiding attribute which says a function hides functions in the base class. If base_check is used and a function hides one from the base class without using hiding it's an error.

But most of the proposal was dropped and only the final and override features were kept, as "identifiers with special meaning" rather than attributes.

Studnia answered 4/11, 2012 at 22:28 Comment(8)
Is this proposal being considered again for a future revision of the standard?Berkly
@Xeo: If nobody is lobbying for it, it won't be looked at. I'd personally just put it into achecker, e.g., based on clang and make checking mandatory.Ingratiating
Doesn't quite answer the question (since although it's not in C++11, Visual C++ might still have some sort of warning I'm not aware of), but still good info, thanks. +1Clash
ah too bad, this would be very helpful for refactoring!Toneme
@BenVoigt, should have been A, it was based on the second example in N2928. Fixed now.Studnia
clang 3.6 has -Winconsistent-missing-override, It already helped me catch situations where some functions were using override, but others were missing. Not as good as what you asked, but it's the best you have out-of-the-box in todays compilers.Alurd
gcc 5.0 has -Wsuggest-override, which is exactly what you're asking for.Brutus
@mic_e: You should put that as an answer. Maybe as -Werror=suggest-override.Clash
G
16

There are few ways to do this in VC++ and equivalent ways with GCC as well.

VC++

Below are the relevant warning numbers in VC++ using Code Quality CppCoreCheck:

[C26435][2] : “Function should specify exactly one of ‘virtual’, ‘override’, or ‘final’.”
[C26433][1] : “Function should be marked with override”

To enable these two warnings:

  1. Open the Property Pages dialog for your project.

  2. Select the Configuration Properties > Code Analysis property page.

  3. Set the Enable Code Analysis on Build and Enable Microsoft Code Analysis properties.

  4. Select the Configuration Properties > Code Analysis > Microsoft property page.

  5. Setup the active rules (e.g.: C++ Core Check Class Rules contains these 2 checkers)

GCC

GCC 5.1+ has added new warning suggest-override that you can pass as command line option -Wsuggest-override.

Clang

Clang 3.5+ has -Winconsistent-missing-override, however this only detects cases if some overriding memebers or base classes use override but other overriding members do not. You might want to take a look at clang-tidy tool as well.

Greenes answered 12/12, 2016 at 21:5 Comment(2)
Those VC++ warnings are for including override where it doesn't apply. The question was asking for warning on a missing override.Quadrifid
And the Clang warning also triggers if you use override on one overridden function but not another inside the same class.Ilise

© 2022 - 2024 — McMap. All rights reserved.