#### Archived

This topic is now archived and is closed to further replies.

# macro to disable unused parameters warning?

This topic is 5064 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I''m programming in Windows and as you may know with WinMain function is required to take a specific set of parameters and a couple of them are rarely used. The problem is I get a "unused parameters warning" whenever I compile my code. I''m a bit of a purist when it comes to warnings. I never disable them with a compiler switch because there have been many times when they''ve helped me catch bugs, and write cleaner/more efficient code. Therefore I don''t want to disable the "unused parameters" warning all together. Is there a macro I can use (ANSI C would be best but even something included via "windows.h" would be OK) that I can wrap around parameter names that I don''t want to be warned about. For example...
int MyFunc(int param1, int param2)
{
TOUCH_PARAMETER(param2);
return (param1 + 1);
}

Any advice would be appreciated. Thanks "Good code will come and go, but bad code sticks around."

##### Share on other sites
Don''t name the parameter and you won''t get a warning:

int whatever(int param1, int, int param3){    use(param1, param3);    return 0;}

This does not produce any warnings.

##### Share on other sites

#define UNUSED( PARAMETER )int MyFunc(int param1, int UNUSED( param2 ) ){    return (param1 + 1);}

##### Share on other sites
You might want to look at UNREFERENCED_PARAMETER (include <windows.h>

##### Share on other sites
Using MSVC, the warning you will get for an unreferenced paramter is 4100. Temporarily turning off this warning (or any warning) is simple:

#pragma warning(disable: 4100) // turn off warning 4100int MyFunc(int param1, int param2){    return (param1 + 1);}#pragma warning(default: 4100) // restore 4100''s warning

##### Share on other sites
I just don''t label the parameter like first reply suggested. If you want to keep a little documentation though, I tend to give the variable a relevent label and comment it out so you know what it is that is being ignored. There really isn''t a need to use macros and using pragma is compiler dependent.

##### Share on other sites
Thanks all for your fast response.
I was fishing for the UNREFERENCED_PARAMETER aluded to by the anonymous poster. I had seen it somewhere in an example but couldn''t remember enough of the name to find the actual macro name via Google.

It''s also interesting to me that leaving a parameter unnamed doesn''t give the warning... not sure if I like that idea. I mean I''m not doubting that it works I''m just thinking if it''s a good thing from a Programming Practices perspecitive. If I did utilize that quirk I think I would go the route of Willm.

Anyway thanks for all the info.

"Good code will come and go, but bad code sticks around."

##### Share on other sites
quote:
Original post by TreborZehn
Thanks all for your fast response.
I was fishing for the UNREFERENCED_PARAMETER aluded to by the anonymous poster. I had seen it somewhere in an example but couldn''t remember enough of the name to find the actual macro name via Google.

It''s also interesting to me that leaving a parameter unnamed doesn''t give the warning... not sure if I like that idea. I mean I''m not doubting that it works I''m just thinking if it''s a good thing from a Programming Practices perspecitive. If I did utilize that quirk I think I would go the route of Willm.

It isn''t a quirk, it''s a feature.

Seriously, that''s one of its legitamate uses. I''ve seen companies that use macros to signal unused variables, other comment out the label. It''s doesn''t make that much difference, although using the windows.h macro will obviously give you a ''dependency'' to Windows which should be avoided.

##### Share on other sites
quote:
Original post by seanw
...using the windows.h macro will obviously give you a ''dependency'' to Windows which should be avoided.
About this ''obvious dependency'' to Windows.... Is it difficult to define UNREFERENCED_PARAMETER your platform or what are you saying? Then maybe you should consider a platform change.

##### Share on other sites
quote:
Original post by Anonymous Poster
About this ''obvious dependency'' to Windows.... Is it difficult to define UNREFERENCED_PARAMETER your platform or what are you saying? Then maybe you should consider a platform change.

No, I just meant you''d be better creating your own macro and placing it in your own header and including that, instead of using the one in windows.h. If you try to compile on a non-windows platform, that macro isn''t going to be available to you. It''s not a big deal, just that you shouldn''t create needless dependencies and this one can easily be avoided. Just good programming practice.

##### Share on other sites
quote:
Original post by TreborZehn
It''s also interesting to me that leaving a parameter unnamed doesn''t give the warning... not sure if I like that idea. I mean I''m not doubting that it works I''m just thinking if it''s a good thing from a Programming Practices perspecitive.

Suppose you''re implementing an interface defined by someone else. Then you might not want to utilize all parameters you get in a function call (while somone else, in another implementation of the same interface, could need all parameters). Therefore it''s really necessary not being forced to use all parameters, and it''s a really valid and useful thing to do. So not having a warning in that case is a good thing.

A static code analyzer, such as LINT, can probably be configured to issue warnings in this case.

##### Share on other sites
quote:
Original post by TreborZehn
It''s also interesting to me that leaving a parameter unnamed doesn''t give the warning... not sure if I like that idea.

Fior MSVC compilers, the default warning level is 3, which doesn''t included warnings about unreferenced parameters and a whole whack of other minor things. I always turn my warning level up to 4 (the highest). Well, that''s not exactly true I guess: sometimes certain 3rd party API''s spew mountains of warnings on level 4, so then I revert back to level 3.

##### Share on other sites
quote:
Original post by BriTeg
Fior MSVC compilers, the default warning level is 3, which doesn''t included warnings about unreferenced parameters and a whole whack of other minor things. I always turn my warning level up to 4 (the highest). Well, that''s not exactly true I guess: sometimes certain 3rd party API''s spew mountains of warnings on level 4, so then I revert back to level 3.

I like to keep the warning level high too, just to stop obscure bugs creeping in. Most compilers have options to set the warning level to be less strict for different files (ie you can set MSVC to less strict warnings only for the 3rd party API''s that have warnings).

##### Share on other sites
In case you''re wondering, there''s no "magic" behind the UNREFERENCED_PARAMETER macro. Here''s the defintion from winnt.h (which gets included via windows.h, so you don''t need to add any extra includes):

#if ! (defined(lint) || defined(_lint))#define UNREFERENCED_PARAMETER(P)          (P)#define DBG_UNREFERENCED_PARAMETER(P)      (P)#define DBG_UNREFERENCED_LOCAL_VARIABLE(V) (V)#else // lint or _lint// Note: lint -e530 says don''t complain about uninitialized variables for// this.  line +e530 turns that checking back on.  Error 527 has to do with// unreachable code.#define UNREFERENCED_PARAMETER(P)          \    /*lint -e527 -e530 */ \    { \        (P) = (P); \    } \    /*lint +e527 +e530 */#define DBG_UNREFERENCED_PARAMETER(P)      \    /*lint -e527 -e530 */ \    { \        (P) = (P); \    } \    /*lint +e527 +e530 */#define DBG_UNREFERENCED_LOCAL_VARIABLE(V) \    /*lint -e527 -e530 */ \    { \        (V) = (V); \    } \    /*lint +e527 +e530 */#endif // lint or _lint

So basically, it''s just doing an assignment statement which the compiler should optimize away.

##### Share on other sites
quote:

In case you''re wondering, there''s no "magic" behind the UNREFERENCED_PARAMETER macro. Here''s the defintion from winnt.h (which gets included via windows.h, so you don''t need to add any extra includes):

...

So basically, it''s just doing an assignment statement which the compiler should optimize away.

Wow, that''s odd; I thought the macro would be defined more like this:
#define UNREFERENCED_PARAMETER(p) /*does nothing*/

I usually just leave out the name if I don''t use it.