I just had a bug (of my own making!) which manifested as follows. I had class with a virtual member function which I needed to extend by adding an extra parameter for some new use cases. I added it, with default values, such that it didn't break any current calls. What I missed was that there was another class which inherited from this class which had an override of this function that was no longer an override. While I'm aware of the override keyword to avoid this when adding new code, I was wondering is there any way of finding all functions that are lexically close enough to be possible cases of a similar bug. Having done this once there is a possibility that I may have done it at some time in the past and would like to retrospectively check the code base.
Asked
Active
Viewed 142 times
4
-
2I am not aware of anything like that, in any C++ compiler. – Sam Varshavchik Jan 24 '19 at 13:06
-
I used to use Lint for this type of thing many years ago, but had thought the compilers did most of that type of checking by now. Maybe not... – SmacL Jan 24 '19 at 13:08
-
1I do not recall lint making any kind of lexical comparison analysis. Current versions of gcc do sort-of do something when complaining about some undeclared identifier -- gcc will try to find something, somewhere, that has a similar name and include "maybe you meant that" in an error message. But, gcc won't proactively warn you about it. Even small C++ programs inherit a boatload of identifiers as soon as any std header gets included, so doing this kind of a check would slow down compilation even more. – Sam Varshavchik Jan 24 '19 at 13:10
-
1@SamVarshavchik: clang has warning for that. – Jarod42 Jan 24 '19 at 13:25
1 Answers
5
Clang has warning flag -Woverloaded-virtual
struct Base
{
virtual void foo(int = 42) {}
};
struct Derived : Base
{
virtual void foo() {} // Oups
};
You might also be interested by clang-tidy with modernize-use-override, to add override
and avoid this mistake in any C++11 compliant compiler in the future.

Jarod42
- 203,559
- 14
- 181
- 302
-
Thanks for this, just installed llvm and clang on VS2017 and while it runs ok on small sample projects, a number of third party libraries I'm using on my main program fail to compile. I'll look at the settings in more detail later on to see if I can figure a workaround. – SmacL Jan 24 '19 at 15:53