Code Yarns โ€๐Ÿ‘จโ€๐Ÿ’ป
Tech Blog โ– Personal Blog

Unused parameter warning of GCC

๐Ÿ“… 2014-Sep-10 โฌฉ โœ๏ธ Ashwin Nanjappa โฌฉ ๐Ÿ“š Archive

GCC throws a unused parameter warning if you are compiling your code with one of these options: -Wunused-parameter, -Wunused or -Wextra.

The warning message looks like this:

foo.cpp:43:40: warning: unused parameter โ€˜offโ€™ [-Wunused-parameter]
 string IntToString(int num, int off)
                                 ^

In most scenarios, this warning is good since you find out that maybe this parameter is not being used currently, so it can be removed from the definition, declaration and the calls.

However, sometimes the function definition cannot be changed. For example, if the function is a callback that is required by a library. In such a case, the function signature cannot be changed even if you are not using that parameter inside your callback.

There is a simple solution for this scenario: remove the parameter name, just keep the parameter type. For example, to fix the function shown above, you could change its definition to:

string IntToString(int num, int)
{
    // Code of function here
}

This keeps the function signature, so it still works with the library. But since the parameter name is gone, you no longer get the warning. ๐Ÿ˜Š

Tried with: GCC 4.9 and Ubuntu 14.04