tags:

views:

2528

answers:

8

I am working on a C++ project and I noticed that we have a number of warnings about unused parameters.

What effect could it have if these warnings are ignored?

+1  A: 

None. except [EDIT]: as others have pointed out, You could have an unassigned output parameter.

You should clean them up, because on many occasions I have seen developers ignore important warnings that were 'hidden' in amongst a large number of warnings, and they were so used to seeing warnings they never paid any attention to them. I try to have zero warnings at all times, and set compiler warnings to the maximum level.

Mitch Wheat
+4  A: 

If you have a whole lot of unimportant warnings that are not important to you, you may overlook the important warning about the line of code that you just wrote that is hiding in the middle of them.

Timbo
This is a very important reason to not ignore warnings. Probably the most important reason.
John Dibling
+1  A: 

It means you wrote a function that takes a parameter but doesn't use the parameter. It's harmless but it might indicate bugs in some cases.

Generally you can silence this warning by removing the parameter name, leaving it anonymous, but that may not be desirable depending on why the parameter is not being used.

I'd suggest you turn off the warning if it is making it harder to find the real problems.

Jonathan
Could you explain how an un-used parameter could "...indicate bugs in some cases." ??
Mitch Wheat
MSalters
+17  A: 

The function with an unused parameter may have a real bug in the following cases:

  1. There is an output parameter, which is not being assigned or written into, resulting in undefined value for the caller.

  2. One of parameters is a callback function pointer, which you must invoke and forget to do so. May happen if there is a lot of #ifdefs in the function.

  3. You declare a local variable with the same name that shadows a parameter and subsequently use the wrong value down in the function.

Not using an input parameters may be harmless, but you can reduce the noise to see useful warnings by marking unused input parameters explicitly in the beginning of the function by casting it to void (works for both C and C++):

#define UNUSED(expr) do { (void)(expr); } while (0)
...

void foo(int param1, int param2)
{
    UNUSED(param2);
    bar(param1);
}

Or omit parameter name (C++ only):

void foo(int param1, int /*param2*/)
{
    bar(param1);
}
Alex B
By the way, approach with UNUSED macro is used in Qt library: it has a Q_UNUSED, that does exactly the same, but without `do..while` wrapper.
Pavel Shved
Indeed it looks like a common idiom. I've seen it in a few places.
Alex B
A: 

If a method doesn't use a parameter then the first question that arises is that why is that parameter a part of the method's signature in the first place. These warnings do make sense since it is bad design that these are referring to and further, there is a little overhead as well that whenever this method is called, this parameter is pushed on the stack so, the best is to refactor the method and remove such parameters which do not have any use.

Having said that, leaving these parameters doesnt harm a lot except for a little overhead that I mentioned.

Aamir
+1  A: 

That depends of if you intended to use the paramater. E.g.

const int Size = 12; // intended for use in some other function

char* MakeBuffer(int size)
{
   return new char[Size];
}

In this code 'size' is unused, and instead the constant 'Size' is being used. So the warning will highlight problems of this type for you.

However, if you never indented to use the parameter then it should just be removed from the method signature. Unless you need to match a signature for a virtual method, or function pointer, if that's the case then you don't have the option to remove it.

Wilka
+1  A: 

In C++ you can have default arguments:

int sum(int first, int second=0){   // should not give warning
    return first+first;
}

You can also have extra argument:

int sum(int first, int second){     // should give warning
    first *= 2;
    return first;
}

If you have a parameter you're not using and it's not defaulted, you should get a warning because you're asking the program to pass extra values to the stack that are never referenced, and therefore is doing more work than it should.

Maybe it means you forgot part of the function logic, too.

warren
I believe the values are still passed to the function if there is no formal parameter. There's just no local name to access the value by.
John Dibling
I'm confused by this example -- even with the default argument, why would you want the second parameter in that example? Whether or not you pass something it will never be used. The only thing I can think of is if you're not using it now but you anticipate the API will need the info in the future.
Joseph Garvin
+6  A: 

Checkers gave a great answer. For a gcc specific way to disable the warning, you can use __attribute__((unused)) like

void foo(int a, int b __attribute__((unused))) {

}

To ignore the second parameter. If your program relies on GCC technologies already, you can use that attribute to be 100% safe from that kind of warning.

Johannes Schaub - litb