What are the consequences of ignoring: warning: unused parameter

前端 未结 8 760
闹比i
闹比i 2020-12-29 23:48

I am working on a C++ project and I noticed that we have a number of warnings about unused parameters.

What effect could it have if these warnings are ignored?

8条回答
  •  别那么骄傲
    2020-12-30 00:17

    The function with an unused parameter may have a real bug in the following cases:

    1. There is an output parameter, which is not being assigned or written into, resulting in undefined value for the caller.

    2. One of parameters is a callback function pointer, which you must invoke and forget to do so. May happen if there is a lot of #ifdefs in the function.

    3. You declare a local variable with the same name that shadows a parameter and subsequently use the wrong value down in the function.

    Not using an input parameters may be harmless, but you can reduce the noise to see useful warnings by marking unused input parameters explicitly in the beginning of the function by casting it to void (works for both C and C++):

    (void)param1;
    

    Or,

    #define UNUSED(expr) do { (void)(expr); } while (0)
    ...
    
    void foo(int param1, int param2)
    {
        UNUSED(param2);
        bar(param1);
    }
    

    Or omit parameter name (C++ only):

    void foo(int param1, int /*param2*/)
    {
        bar(param1);
    }
    

提交回复
热议问题