57

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
  • 60,915
  • 9
  • 122
  • 205
user541686
  • 197,378
  • 118
  • 507
  • 856
  • 12
    Why is this question being closed? Is there something wrong with questions that are about C++ compiler warnings? (If the answer is "no" then that's a perfectly valid answer after all..) – user541686 Nov 04 '12 at 22:17
  • 13
    @Mehrdad: ___I don't think there's anything wrong with this question.___ If it really gets closed, it should be reopened. – sbi Nov 04 '12 at 22:19
  • @sbi: Thanks, glad to hear that. – user541686 Nov 04 '12 at 22:23
  • 5
    To answer your question concretely, no, VC++ 2012 RTM contains no such warning (even disabled by default). – ildjarn Nov 06 '12 at 23:32
  • @ildjarn: Thanks, that's the kind of answer I was looking for. :) – user541686 Nov 06 '12 at 23:33

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
  • 160,213
  • 23
  • 318
  • 501
  • 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
14

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++:

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.

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.

Drew Dormann
  • 54,920
  • 13
  • 119
  • 171
Shital Shah
  • 55,892
  • 12
  • 218
  • 175
  • 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