Requiring virtual function overrides to use override keyword
Solution 1:
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
.
Solution 2:
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.