代码之家  ›  专栏  ›  技术社区  ›  Phil Hannent

如何最好地消除关于未使用变量的警告?

  •  319
  • Phil Hannent  · 技术社区  · 16 年前

    #ifdef _MSC_VER
    void ProcessOps::sendToExternalApp(QString sAppName, QString sImagePath, qreal qrLeft, qreal qrTop, qreal qrWidth, qreal qrHeight)
    #else
    void ProcessOps::sendToExternalApp(QString sAppName, QString sImagePath, qreal /*qrLeft*/, qreal /*qrTop*/, qreal /*qrWidth*/, qreal /*qrHeight*/)
    #endif
    {
    

    23 回复  |  直到 8 年前
        1
  •  270
  •   Community Mohan Dere    9 年前

    put it in " (void)var; " expression (什么也不做),这样编译器就会看到它被使用了。这在编译器之间是可移植的。

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

    或者,

    #define UNUSED(expr) do { (void)(expr); } while (0)
    ...
    
    void foo(int param1, int param2)
    {
        UNUSED(param2);
        bar(param1);
    }
    
        2
  •  84
  •   Fabio says Reinstate Monica MoMoTaur    8 年前

    __attribute__((unused)) 预处理器指令以实现您的目标。

    int foo (__attribute__((unused)) int bar) {
       return 0;
    }
    
        3
  •  34
  •   alex tingle    16 年前
        4
  •  29
  •   scx    9 年前

    C++17更新

    它被覆盖在 [dcl.attr.unused]

    在每个属性列表中最多出现一次,并且不应出现属性参数子句。 ...

    例子:

     [[maybe_unused]] void f([[maybe_unused]] bool thing1,
                            [[maybe_unused]] bool thing2) {
      [[maybe_unused]] bool b = thing1 && thing2;
        assert(b);
     }
    

    int foo ( int bar) {
        bool unused_bool ;
        return 0;
    }
    

    clang和gcc都使用以下命令生成诊断 -墙-韦克斯特拉 对两者 酒吧 未使用的工具 ( See it live ).

    使诊断静音:

    int foo ([[maybe_unused]] int bar) {
        [[maybe_unused]] bool unused_bool ;
        return 0;
    }
    

    see it live .

    C++17之前

    在C++11中 UNUSED 宏可以使用lambda表达式形成( via Ben Deane )捕获未使用的变量:

    #define UNUSED(x) [&x]{}()
    

    给定以下示例,应该优化lambda表达式的立即调用:

    int foo (int bar) {
        UNUSED(bar) ;
        return 0;
    }
    

    我们可以看到 godbolt

    foo(int):
    xorl    %eax, %eax
    ret
    
        5
  •  23
  •   user194119    16 年前

        6
  •  22
  •   Digital Trauma    7 年前

    一种更简洁的方法是注释掉变量名:

    int main(int /* argc */, char const** /* argv */) {
      return 0;
    }
    
        7
  •  21
  •   Marcin Wyszynski    12 年前

    -Wunused-parameter -Wall -Wextra

    -Wno-unused-parameter

        8
  •  16
  •   Shafik Yaghmour    8 年前

    here

    #ifdef UNUSED
    #elif defined(__GNUC__) 
    # define UNUSED(x) UNUSED_ ## x __attribute__((unused)) 
    #elif defined(__LCLINT__) 
    # define UNUSED(x) /*@unused@*/ x 
    #else 
    # define UNUSED(x) x 
    #endif
    
    void dcc_mon_siginfo_handler(int UNUSED(whatsig))
    
        9
  •  11
  •   Philippe    9 年前

    一个或多个 未使用的参数:

    template <typename... Args> inline void unused(Args&&...) {}
    
    int main(int argc, char* argv[])
    {
        unused(argc, argv);
        return 0;
    }
    
        10
  •  7
  •   Ben Dadsetan    16 年前

    (void)param2

    void foo(int param1, int param2)
    {
        std::ignore = param2;
        bar(param1);
    }
    

        11
  •  6
  •   rioki    16 年前

    在充分尊重C++17的前提下 C++ Core Guidelines

        12
  •  4
  •   Michael Krelin - hacker    16 年前

    在大多数情况下,使用预处理器指令被认为是邪恶的。理想情况下,你想像害虫一样避开它们。记住,让编译器理解你的代码很容易,让其他程序员理解你的程序要困难得多。这里和那里有几十个这样的案例,以后很难为自己或现在的其他人阅读。

    一种方法可能是将参数组合到某种参数类中。然后,您可以只使用变量的一个子集(相当于实际分配0),或者为每个平台对该参数类进行不同的专门化。然而,这可能不值得,你需要分析它是否合适。

        13
  •  2
  •   Joshua    16 年前

    首先,警告是由源文件中的变量定义而不是头文件生成的。头部可以保持原始并且应该保持原始,因为您可能正在使用类似doxygen的东西来生成API-文档。

    例子:

    func(int a, int b)
    {
        b;
        foo(a);
    }
    

    这可能看起来很神秘,所以定义了一个类似于“未使用”的宏。MFC的做法是:

    #ifdef _DEBUG
    #define UNUSED(x)
    #else
    #define UNUSED(x) x
    #endif
    

    这样,您在调试版本中仍然可以看到警告,这可能会有所帮助。

        14
  •  2
  •   OlDor    10 年前

    #ifdef _MSC_VER
    # define P_(n) n
    #else
    # define P_(n)
    #endif
    
    void ProcessOps::sendToExternalApp(
        QString sAppName, QString sImagePath,
        qreal P_(qrLeft), qreal P_(qrTop), qreal P_(qrWidth), qreal P_(qrHeight))
    

        15
  •  2
  •   stacker    9 年前

    UNREFERENCED_PARAMETER(p)

    UNREFERENCED PARAMETER(p) 被定义为

    #define UNREFERENCED_PARAMETER(P)          (P)
    

    在WinNT.h。

        16
  •  1
  •   Nampo    8 年前

    使用编译器的标志,例如GCC的标志: -Wno-unused-variable

        17
  •  1
  •   Gian Lorenzo Meocci    7 年前

    在C++11中,这是我使用的解决方案:

    template<typename... Ts> inline void Unreferenced(Ts&&...) {}
    
    int Foo(int bar) 
    {
        Unreferenced(bar);
        return 0;
    }
    
    int Foo2(int bar1, int bar2) 
    {
        Unreferenced(bar1, bar2);
        return 0;
    }
    

    如果额外的代码是一个问题,您可以使用此声明:

    (decltype(Unreferenced(bar1, bar2)))0;
    

    但此时,宏提供了更好的可读性:

    #define UNREFERENCED(...) { (decltype(Unreferenced(__VA_ARGS__)))0; }
    
        18
  •  0
  •   FaceBro    8 年前

    这很有效,但需要 C++11

    template <typename ...Args>
    void unused(Args&& ...args)
    {
      (void)(sizeof...(args));
    }
    
        19
  •  -12
  •   Tobias Langner    16 年前

    我发现大多数给出的答案只适用于局部未使用的变量,并会导致未使用的静态全局变量的编译错误。

    template <typename T>
    const T* UNUSED_VARIABLE(const T& dummy) { 
        return &dummy;
    }
    #define UNUSED_GLOBAL_VARIABLE(x) namespace {\
        const auto dummy = UNUSED_VARIABLE(x);\
    }
    
    static int a = 0;
    UNUSED_GLOBAL_VARIABLE(a);
    
    int main ()
    {
        int b = 3;
        UNUSED_VARIABLE(b);
        return 0;
    }
    

    这是有效的,因为匿名命名空间中的非静态全局变量不会报告任何警告。

    C++11是必需的

     g++  -Wall -O3  -std=c++11 test.cpp