Warn when using parameter marked as __unused

前端 未结 2 1144
独厮守ぢ
独厮守ぢ 2021-02-06 05:56

Using the -Wunused-parameter flag, you can enforce __unused for unused parameters, as a compiler optimization. The following code causes two warnings:

#include &         


        
2条回答
  •  逝去的感伤
    2021-02-06 06:23

    The __unused attribute is intended to prevent complaints when arguments to functions/methods or functions/methods are unused, not to enforce their lack of use.

    The term used in the GCC manual is:

    This attribute, attached to a function, means that the function is meant to be possibly unused

    And for variables:

    This attribute, attached to a variable, means that the variable is meant to be possibly unused.

    The most common use is for development against an interface - e.g. callbacks, where you may be forced to accept several parameters but don't make use of all of them.

    I use it a bit when I'm doing test driven development - my initial routine takes some parameters and does nothing, so all the parameters take __attribute__((unused)). As I develop it I make use of the parameters. At the end of development I remove them from the method, and see what shakes out.

提交回复
热议问题