Avoid Warning 'Unreferenced Formal Parameter'

Avoid warning 'Unreferenced Formal Parameter'

In C++ you don't have to give a parameter that you aren't using a name so you can just do this:

void Child::Function(int)
{
//Do nothing
}

You may wish to keep the parameter name in the declaration in the header file by way of documentation, though. The empty statement (;) is also unnecessary.

Avoid warning 'Unreferenced Formal Parameter' with default argument

The usual way is to not name the parameter, just like you said:

virtual int foo(bool = false)
{
return -1;
}

You can also deliberately ignore a value by casting it to void:

(void)status;

C4100 Unreferenced Formal Parameter when used in a Template

Based on the instance name, it looks like factory might be a singleton, and instance is a static function. In that case, the value of factory is indeed never used, and you can instead call the function directly on the class, which is essentially what the compiler does anyway:

Factory::instance().Register(...);

Then you can remove the parameter entirely, and you'll avoid the apparent compiler bug that conflates usage of the parameter with usage of the parameter's value.

Why should unreferenced formal parameter worry me?

To prevent possible distractions (forgetting to use the parameter) and to strive for a more maintainable code are reasons enough.

The possible performance hit of unused parameters being copied is an additional reason, but not the main one.

How to deal with Warning C4100 in Visual Studio 2008

If the parameters are unreferenced, you can leave them unnamed:

int main(int, char**)
{
}

instead of

int main(int argc, char** argv)
{
}

If you really want just to suppress the warning, you can do so using the /wd4100 command line option to the compiler or using #pragma warning(disable: 4100) in your code.

This is a level 4 warning; if you compile at a lower warning level, you won't get this warning. The warning level is set in the project properties (right-click project, select Properties; on the Configuration Properties -> C++ -> General, set "Warning Level").

Will the compiler do different things depending on how I handle unreferenced parameters?

I can see no reason why the compiler would treat any of those different. But the only way to tell for sure for your compiler is for you to look at the output of your compiler.

I would prefer the first option since this situation (unused parameters) is what that language feature was designed for.



Related Topics



Leave a reply



Submit