c++visual-studioparametersmacroscompiler-warnings

Using UNREFERENCED_PARAMETER macro


I'm using \W4 warning level on Visual Studio and I'm writing a Windows program.

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

All these parameters are not used in my application, so I get warnings at compile time.

I know there are two ways of dealing with this:

  1. Commenting parameters HINSTANCE /*hInstance*/ ...
  2. Using the UNREFERENCED_PARAMETER macro

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
      UNREFERENCED_PARAMETER(hInstance);
      UNREFERENCED_PARAMETER(hPrevInstance);
      UNREFERENCED_PARAMETER(lpCmdLine);
      UNREFERENCED_PARAMETER(nCmdShow);
    

Which one is the correct one? Which one is the safer to use? Are there any problems with using the macro?


Solution

  • I would prefer commenting the parameters.

    The macro UNREFERENCED_PARAMETER is defined in winnt.h and therefore not portable.

    And if later you do reference it, you might overlook to remove the macro.

    Edit: With C++17 you can now use the [[maybe_unused]] attribute. This is useful for code depending on preprocessor macros:

    void foo( [[maybe_unused]] int value )
    {
    #ifdef USE_VALUE
       useValue(value);
    #endif
    }
    

    Now there won't be warnings even if USE_VALUE is undefined.