Search code examples
c++c++11unused-variables

Unused parameter in c++11


In c++03 and earlier to disable compiler warning about unused parameter I usually use such code:

#define UNUSED(expr) do { (void)(expr); } while (0)

For example

int main(int argc, char *argv[])
{
    UNUSED(argc);
    UNUSED(argv);

    return 0;
}

But macros are not best practice for c++, so. Does any better solution appear with c++11 standard? I mean can I get rid of macros?

Thanks for all!


Solution

  • I have used a function with an empty body for that purpose:

    template <typename T>
    void ignore(T &&)
    { }
    
    void f(int a, int b)
    {
      ignore(a);
      ignore(b);
      return;
    }
    

    I expect any serious compiler to optimize the function call away and it silences warnings for me.