I want to write a virtual function with an empty body in C++.
These are the ways I know:
virtual void f(int a, int b, int c) { }
If I write like this, the compiler will give a warning about unused parameter. I don't want to globally suppress this kind of warning since it is still useful in other cases.
This question How do I best silence a warning about unused variables? talked about how to suppress unused parameters, but those solutions have shortcomings as described below:
virtual void f(int a, int b, int c) { (void) a; (void) b; (void) c; }
If I write like this, I have to write as many (void)
's as the number of parameters. If there are 50 functions and each of them has 10 parameters, I have to manually type 500 (void)
's, which is tedious. In addition, each time I add or delete a formal parameter, the corresponding (void)
statement also needs to be added or deleted. It's a contradiction to the "don't repeat yourself": two places have to be modified every time when one change happens.
virtual void f(int /*a*/, int /*b*/, int /*c*/) { }
If I write like this, when I write a function call, the IDE will fail to hint the formal parameter's names but just types; and when I override this function in a derived class, the IDE also fails to provide a declaration with the names of these parameters.
virtual void f(int a, int b, int c) = 0;
If I write like this, the derived class has to override this function, which is not what I want.
I hope if there were something like this:
virtual void f(int a, int b, int c) = default;
Is there such a language feature, best practice, or workaround?
Thanks!