How to Remove Unreferenced Formal Parameter warning?

26 11 2008

Hands on legacy code base is a different kind of game! You have to learn a lot of tricks. During old days, legacy codebases have the compiler default settings. But now, for better error catching, most will switch  the warning level to high. Well, did you ever ported your legacy codebase by updating its project settings such as by switching the warning level from Level 3 (default) to Level 4, most probably you’ll get this – “Warning C4100: ‘Param2’ : unreferenced formal parameter”.

In most of the case, it might be some unused stack variable. But in some other cases, it might be the unused function parameters as well. For instance, if base class have some pure virtual function, we have to implement that, even if we don’t have any specific implementation for it. In that case, the parameters will not be used and can cause the same warning. Well how to “kick out” those warnings easily?

unreferrencedvariablewarning


Well, there are two tricks.

1) Use the macro – UNREFERENCED_PARAMETER
The common trick is to use the macro UNREFERENCED_PARAMETER(). When it expands, it just assigns the variable to itself. Just like a=a. Which will remove the warning, since the variable is used to assign to itself.

// The function that derived class was forced to implement,
// since its pure virtual function defined in base.
void CRabbitDialogDlg::Function( bool Param1, bool Param2 )
{
    // Here Param1 and Param2 are not used which will
    // trigger warning. Now they won't.
    UNREFERENCED_PARAMETER( Param1 );
    UNREFERENCED_PARAMETER( Param2 );
}

2) Comment out the variable names.
Even though UNREFERENCED_PARAMETER() serves the need, its not efficient. isn’t it? It assigns the variable to itself and thus avoids the warning. Well there is another easy method. Just comment out the variable name. Check the code snippet below.

// The function that derived class was forced to implement,
// since its pure virtual function defined in base.
void CRabbitDialogDlg::Function( bool /*Param1*/, bool /*Param2*/ )
{
    // Since the variable name doesn't exist, how compiler can
    // complain that variable is not used. ;)
}


In one of my projects, I was forced write an empty implementation for a pure virtual function in derived class. And my client needs zero compilation warning. At that time I really struggled to remove this warning. 😀 Now when i shared it with you, it feels good!


Targeted Audiance – Beginners.

Advertisements

Actions

Information

4 responses

2 12 2008
Antonio

use #pragma warning (disable: xxxx yyyy)

use #pragma warning (deafult: xxxx yyyy)

2 12 2008
Jijo.Raj

You could use #pragma to hide the warnings. But I think, its not good to shut the mouth of compiler rather than fixing it.

Anyway thanks for sharing it. Keep watching! 🙂

Regards,
Jijo.

5 12 2008
cppkid

Use UNUSED_ALWAYS

5 12 2008
Jijo.Raj

Thats great! Thanks for sharing it. Keep Watching.

Regards,
Jijo.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s




%d bloggers like this: