How to avoid compiler warning with unused parameters

There may be unused formal parameters for a function. This is quite natural in the case of polymorphism, where one of the derived classes may use a parameter which is not at all needed in other derived classes. But you may be forced to pass it (of course, dummy value) in the base class (and in all the derived classes indeed). Now in all the derived classes where the parameter is not at all used, the compiler will pop up with a warning – “unused parameter!!!”.

Although this is harmless, I would avoid the warning in the following way.

void MyClass::MyFunction(bool nonUsedParameter)

{

UNREFERENCED_PARAMETER(nonUsedParameter);

// The code continues

}

A better altenative is to code as follows.

void MyClass::MyFunction(bool /*nonUsedParameter*/)

{

// The code continues

}

Another method is to use the macro UNUSED_ALWAYS, defined in afx.h (Generously informed by Nibu, see comments) instead of UNUSED_PARAMETER.

Advertisements

3 Responses to How to avoid compiler warning with unused parameters

  1. Nibu Thomas says:

    There is one more called UNUSED_ALWAYS. 🙂

  2. cppkid says:

    Thanks Nibu…..I’m blessed to have support from a Microsoft MVP. 🙂

  3. Boris Dušek says:

    Much better in C++ (easier to use, portable) is to simply omit the formal parameter name in the function/method declaration (or comment it out, so that you know what is was):

    void MyClass::MyFunction(bool /*nonUsedParameter*/)

    I often used it for main until I discovered int main(void) is also allowed:

    int main(int, char **) {

    }

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: