2 minutes
Unused Parameters in C++
Just some quick thought on how to handle compiler warnings about unused parameters in C++ code. While these warnings are helpful most of the time, sometimes you simply have to ignore a parameter that is required by an interface definition. Then you got three solutions to remove the warning:
Change the compiler flags. The worst solution. In other cases these warnings are really helpful. Generally I really like to compile with the highest warning level. Most of the warnings (at least for GCC) really have something to tell.
Use a macro to flag a variable as being unused, e.g. from Qt:
foo(int aParameter) { Q_UNUSED(aParameter) }
Even though this looks like a reasonable solution it has the drawback of having a potential for confusion. Maybe sometime later you need the parameter but forget to remove the Q_UNUSED macro, hence generating something like this:
foo(int aParameter) { Q_UNUSED(aParameter) // 30 lines of other method code int anotherVariable = 30 * aParameter; }
Suddenly your code documents that a variable isn’t used but actually it is and the compiler can’t detect this error.
Simply comment the variable name:
foo(int /*aParameter*/) { // 30 lines of other method code int anotherVariable = 30 * aParameter; }
Now the warning is gone, too and the compiler can detect either the illegal use of the variable in line three or the illegal statement that this variable is unused by purpose.