Visual C++: Warning C4100

Warning 4100: unreferenced formal parameter might appear when C++ code is compiled at Warning Level 4 (/W4) with the Visual C++ compiler. For example, a function that generates C4100:

void Foo::OnEvent( const FooEvent& event )
{
    return;
}

There are a few ways to deal with this warning:

  • If this warning made you aware that you had a redundant parameter, that is good! Remove the unused parameter from both the function declaration and the definition:
    void Foo::OnEvent()
    {
        return;
    }
    
  • If the function signature cannot be changed but you do not really use the parameter in the function body, then remove the parameter name, while retaining the parameter type:
    void Foo::OnEvent( const FooEvent& )
    {
        return;
    }
    
  • If you are playing around with the code in the function body and will need the parameter in the future, then comment the parameter temporarily:
    void Foo::OnEvent( const FooEvent& /* event */ )
    {
        // Some commented code that uses "event" parameter
        return;
    }
    
  • pragma warning directives can be used to disable the warning locally:
    #pragma warning( push )
    #pragma warning( disable: 4100 )
    void Foo::OnEvent( const FooEvent& event )
    {
        return;
    }
    #pragma warning( pop )
    
  • If you are having many such functions generating warnings and want to do nothing to fix them, then use a pragma warning directive over the entire source file:
    // Foo.cpp
    #pragma warning( disable: 4100 )  // Disable for entire source file Foo.cpp
    
    void Foo::OnEvent( const FooEvent& event )
    {
        return;
    }
    

A final note: Visual C++ 2010 does not seem to have this warning at all! It has been mysteriously removed without providing any reason πŸ™‚

One thought on “Visual C++: Warning C4100

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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