Dealing with C++ "Unused Parameter" Warnings
Generally, when I am working with clients I try to get everyone on the team to agree to a "no compiler warnings" policy and immediately turn on the "warnings as errors" switch. Thereafter, any compiler warning is automatically treated like an error and the build will fail.
Unfortunately, when stepping into a project where most of the code was written long ago, there may be hundreds if not thousands of compiler warnings. The only solution is to investigate and fix (or suppress) each one.
So how do you suppress the unused parameter warnings when you legitimately don't use the argument? There are three ways:
1. #pragma unused. #pragmas are compiler specific and should be avoided. However, if your compiler supports it, you can use it as follows:
3. Cast to void. Casting an unused variable to void will always
stop the warning.
void my_function(int32 foo)
{
#pragma unused foo;
}
2. Comment the Argument. Additionally, you can comment out the argument. The compiler will not give you an unused warning:
void myfunction(int /* arg */)
{
}
3. Cast to void. Casting an unused variable to void will always
stop the warning.
#define UNUSED_ARGUMENT(x) (void)x
void myfunction(int arg )
{
UNUSED_ARGUMENT(arg);
}
Labels: C++, Programming
What if the argument has a default value, but is not used?
like
function(bool foo=true) ?
Posted by
Anonymous |
April 27, 2009 1:21 AM
> function(bool foo=true) ?
comment out the value?
Posted by
Diego |
June 5, 2009 7:57 PM
I use the macro here to handle unused parameter warnings.
Posted by
mdburton |
November 3, 2009 2:34 PM
#define UNUSED_ARG (x)
void myfunction(int UNUSED_ARG(arg) )
{
…
}
Posted by
Anonymous |
November 12, 2009 8:39 AM
Bonjorno, www.agavemountain.com!
[url=http://doveacquistarecialisonlinerp.pun.pl/ ]Compra cialis [/url] [url=http://acquistoviagraonlineta.pun.pl/ ]Acquisto viagra generico[/url] [url=http://acquistolevitragenericomp.pun.pl/ ]Comprare levitra online[/url] [url=http://doveacquistarecialistr.pun.pl/ ]Compra cialis online[/url] [url=http://venditaviagrati.pun.pl/ ]Acquisto viagra [/url] [url=http://compracialisco.pun.pl/ ]Vendita cialis [/url]
Posted by
Anonymous |
November 28, 2009 9:28 PM