c++ virtual function with arguments getting warnings when unused

Solution 1:

Simply don't give them a name:

virtual void myFunc( int&, int& );

Solution 2:

Since you don't want to use them you can emit the parameter names.

However, instead of removing them completely it's sometimes more useful to comment them out like this:

virtual void myFunc(int& /* a */ , int& /* b */ ) 
{
}

This way you can still see what the intent of the parameter was by looking at the commented out name. This is particularly useful if you put the implementation in the header as it will be the only place which mentions the parameter names.

Solution 3:

You have several ways to silent this warning:

  • Remove them from declaration/definition:

    virtual void myFunc(int& /* a */ , int& /* b */ ) {}
    

    This solution may provoke some warnings with some tool as Doxygen...

  • Use a trick to tell the argument is unused:

    template <typename T> void unusedArg(const T&) {} // Helper function.
    
    // In the class
    virtual void myFunc(int& a, int& b) { unusedArg(a); unusedArg(b); }
    

    or in C++11:

    template <typename... Ts> void unusedArgs(const Ts&...) {} // Helper function
    
    // In the class
    virtual void myFunc(int& a, int& b) { unusedArgs(a, b); } // C++11
    
  • In C++17, you may also use attribute [[maybe_unused]]:

    // In the class
    virtual void myFunc([[maybe_unused]] int& a, [maybe_unused]] int& b) {}