Suppress Unreferenced Parameter/Variable Warnings in Windows C/C++ Applications

I use warning level 4 (/W4) for all of my projects as well as “warnings as errors” (/WX) on Release builds. During development I often run into C4100 warnings for unreferenced parameters or variables that are either:

  1. Currently unused but will be used once development is complete
  2. Due to a specific required signature (e.g. for use in a Windows callback in an API) but that I don’t need

In the past I’ve just used a cast to suppress the warnings like this:

// Suppress C4100 Warnings for Unused Parameters
(void*) param1;
(void*) param2;

I came across a collection of handy macros today in WinNT.h (you should include <Windows.h>):

//
// Macros used to eliminate compiler warning generated when formal
// parameters or local variables are not declared.
//
// Use DBG_UNREFERENCED_PARAMETER() when a parameter is not yet
// referenced but will be once the module is completely developed.
//
// Use DBG_UNREFERENCED_LOCAL_VARIABLE() when a local variable is not yet
// referenced but will be once the module is completely developed.
//
// Use UNREFERENCED_PARAMETER() if a parameter will never be referenced.
//
// DBG_UNREFERENCED_PARAMETER and DBG_UNREFERENCED_LOCAL_VARIABLE will
// eventually be made into a null macro to help determine whether there
// is unfinished work.
//

#if ! defined(lint)
#define UNREFERENCED_PARAMETER(P)          (P)
#define DBG_UNREFERENCED_PARAMETER(P)      (P)
#define DBG_UNREFERENCED_LOCAL_VARIABLE(V) (V)

Comments

2 responses to “Suppress Unreferenced Parameter/Variable Warnings in Windows C/C++ Applications”

  1. Oliver Avatar
    Oliver

    For C++ you can also leave off the parameter name. Using Visual Assist X it’s no more than marking the name and pressing *.

    So from this:

    int someFunc(void* param1, void* param2);

    to:

    int someFunc(void* /*param1*/, void* /*param2*/);

    However, this won’t work with C compilers below the C99 standard, I think. Works fine in any C++ code, though.

  2. ZachB Avatar

    @Oliver that’s pretty slick. I’ll have to try that out!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.