We have the code:

struct B { virtual void f() {} }; struct D : B { void f(int) { } }; int main() { } 

clang issues a warning to it:

 'D::f' hides overloaded virtual function [-Woverloaded-virtual] 

Now we make f(int) static in class D :

 struct B { virtual void f() {} }; struct D : B { static void f(int) { } }; int main() { } 

The code is compiled without warning. However, an attempt to use f() on an object of class D :

 int main() { D d; df(); } 

leads to an error:

 too few arguments to function call, expected 1, have 0 

Those. static f(int) still hides the function from B The problem is solved by adding using B::f; in class D and the code compiles successfully . But the question arises:

Are there any objective reasons not to issue a warning while hiding the B::f() function of a static D::f(int) ?

  • IMHO, the fact is that hiding a function is not in itself an error, and the compiler should not wipe every time it happens. But the accidental hiding of a virtual function instead of overriding, due to an incorrect signature, already looks like an error. - yrHeTaTeJlb
  • @yrHeTaTeJlb is a reasonable assumption, but now there is an override to avoid it. Even in favor of random hiding, an explicit virtual could speak (I don’t have it in the first code example), but if the signatures coincided, it would be implicitly assumed. - αλεχολυτ
  • Gcc has a -Wsuggest-override flag forcing to use override. Maybe there will be no warning with him? - int3

1 answer 1

As far as I understand, this warning is necessary in order to prevent errors related to the concealment of a virtual function when its redefinition was required. For example, with a typo:

 struct chart; struct Base{ virtual void* get(char* e); }; struct Derived: public Base{ void* get(chart* e); //Ой }; 

The override could solve this problem, but as long as it is optional, this diagnostics will live and work from time to time.

As for static . I think the compiler developers reasoned that since the virtual functions cannot be static, the programmer most likely did not plan to override anything. Accordingly, the error is most likely not.

  • It seems I’ve seen this chart somewhere with the chart - αλεχολυτ
  • @alexolut, maybe here . Stumbled while trying to find a description of all the varnings, and could not go through mimio - yrHeTaTeJlb