未检测到未使用的变量 [英] A variable not detected as not used

查看:155
本文介绍了未检测到未使用的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用g ++ 4.3.0来编译这个例子:

I am using g++ 4.3.0 to compile this example :

#include <vector>

int main()
{
  std::vector< int > a;
  int b;
}

如果我以最大警告级别编译示例,变量 b 未使用:

If I compile the example with maximum warning level, I get a warning that the variable b is not used :

[vladimir@juniper data_create]$ g++ m.cpp -Wall -Wextra -ansi -pedantic
m.cpp: In function ‘int main()’:
m.cpp:7: warning: unused variable ‘b’
[vladimir@juniper data_create]$

问题是:为什么变量 a 没有被报告为不是用过的?
我必须传递哪些参数才能获取变量 a 的警告?

The question is : why the variable a is not reported as not used? What parameters do I have to pass to get the warning for the variable a?

推荐答案

<理论上, std :: vector< int> 的默认构造函数可能会有任意的副作用,因此编译器无法确定是否移除 a 会改变程序的语义。

In theory, the default constructor for std::vector<int> could have arbitrary side effects, so the compiler cannot figure out whether removing the definition of a would change the semantics of the program. You only get those warning for built-in types.

一个更好的例子是一个锁:

A better example is a lock:

{
    lock a;
    // ...
    // do critical stuff
    // a is never used here
    // ...
    // lock is automatically released by a's destructor (RAII)
}

即使 a 在定义之后从不使用,删除第一行会出错。

Even though a is never used after its definition, removing the first line would be wrong.

这篇关于未检测到未使用的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆