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.)

ildjarn
  • 62,044
  • 9
  • 127
  • 211
user541686
  • 205,094
  • 128
  • 528
  • 886

2 Answers2

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.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • 1
    Is this proposal being considered again for a future revision of the standard? – Xeo Nov 04 '12 at 22:33
  • 3
    @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. – Dietmar Kühl Nov 04 '12 at 22:38
  • 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. +1 – user541686 Nov 04 '12 at 22:42
  • ah too bad, this would be very helpful for refactoring! – peter karasev Apr 25 '14 at 15:41
  • @BenVoigt, should have been `A`, it was based on the second example in N2928. Fixed now. – Jonathan Wakely Jul 28 '14 at 17:36
  • 8
    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. – Sergio Martins Jan 27 '15 at 22:30
  • 14
    gcc 5.0 has -Wsuggest-override, which is exactly what you're asking for. – mic_e Jul 18 '15 at 19:22
  • @mic_e: You should put that as an answer. Maybe as `-Werror=suggest-override`. – user541686 Jul 06 '23 at 08:05
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.

mckelvin
  • 3,918
  • 1
  • 29
  • 22
Shital Shah
  • 63,284
  • 17
  • 238
  • 185
  • 12
    Those VC++ warnings are for _including_ override where it doesn't apply. The question was asking for warning on a _missing_ override. – mskfisher Jan 31 '17 at 18:35
  • And the Clang warning also triggers if you use override on one overridden function but not another inside the same class. – rubenvb Jan 14 '19 at 08:08