Dave's Brain

Browse - Programming Tips - How should I avoid a warning about an unreferenced parameter?

Date: 2014jul25
Platform: Windows

Q.  How should I avoid a warning about an unreferenced parameter?

A.  There are four ways.

1.  Globally turn off the warning - UNSAFE
2.  Use pragma to turn off the warning - NOT SO PRETTY
3.  Remove the name of the parameter
4.  Use the UNREFERENCED_PARAMETER() macro

I'll show the last two ways since those are what I prefer.
An example of code that will get the warning:

BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved) { }
3. Remove the name of the parameters:
BOOL APIENTRY DllMain(HMODULE, DWORD, LPVOID) { }
This is ok but then you don't get the name in tool tips. But if the type describes the parameter you are fine. 4. Use the macro:
BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved) { UNREFERENCED_PARAMETER(hModule); UNREFERENCED_PARAMETER(fdwReason); UNREFERENCED_PARAMETER(lpReserved); }
Or use a mix:
BOOL APIENTRY DllMain(HMODULE, DWORD fdwReason, LPVOID) { UNREFERENCED_PARAMETER(fdwReason); }
Here, we remove the name for the first parameter since HMODULE describes it. And the last parameter isn't used. But DWORD doesn't describe the second parameter so we use the macro.

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.