g++ does not show a #39;unused#39; warning(g++ 不显示“未使用警告)
问题描述
我有一小段 C++ 代码:
I have a little piece of code in C++:
#include <iostream>
#include <iterator>
#include <string>
using namespace std;
int main() {
int i=0;
istream_iterator<string> EOS;
double x;
return 0;
}
现在我用我的 g++ (GCC) 4.4.4 编译它
Now i compile it with my g++ (GCC) 4.4.4
g++ -W -Wall -pedantic test.cc -o test
得到:
test.cc: In function 'int main()':
test.cc:9: warning: unused variable 'i'
test.cc:11: warning: unused variable 'x'
为什么未使用的 EOS 没有警告?
Why there is no warning for unused EOS?
推荐答案
它不是原始值,因此它的构造函数和/或析构函数可能会产生预期的副作用.
It is not a primitive value, so its constructor and/or destructor might have desired side effects.
为了说明这在实践中发生:我使用一个类来对代码段进行计时,大致如下:
To illustrate that this happens in practice: I use a class to time sections of code, that looks roughly like this:
class Timed {
double start;
public:
Timed() { start = now(); }
~Timed() { std::cout << (now() - start) << '
'; }
}
所以要衡量一个函数需要多长时间,我只是这样做:
So to measure how long a function takes, I simply do:
void slow() {
Timed t;
// heavy operation here...
}
变量 t
永远不会被使用,但它对代码的行为仍然很重要.
The variable t
never gets used, but it's still important to the behaviour of the code.
这篇关于g++ 不显示“未使用"警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:g++ 不显示“未使用"警告


基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01