为什么-Winit-self与-Wuninitialized分离 [英] Why is -Winit-self separate from -Wuninitialized
问题描述
这个问题是关于让gcc编译器在你输入一个拼写错误并初始化一个变量时发出警告。
int f()
{
int i = i;
返回i;
}
事实证明,您需要 -Winit-self $ c除了
-Wuninitialized
:
code> -Winit-self (仅限C,C ++,Objective-C和Objective-C ++)警告使用自己初始化的未初始化变量。注意
此选项只能与-Wuninitialized
选项一起使用,该选项仅在
turn中适用于-O1
及以上。
我的问题是:为什么这不是 -Wuninitialized
的默认行为, ?什么是你想警告未初始化变量的用例,但不是自初始化变量,这同样很麻烦? 解决方案
p>它看起来像这个错误报告警告使用自己初始化的成员变量有一个解释(强调我的):
我同意Andrew的a(a)错误应该总是警告,它应该独立于-Winit-self,它存在,以便 - 初始化不会警告自动初始化自动变量以消除警告的常见(但可疑)练习。
它可能被称为一个可疑的做法,因为它是,错误报告是一个C ++错误报告。
This Question is about getting the gcc compiler to warn when you make a typo and initialize a variable with itself.
int f() { int i = i; return i; }
It turns out you need the -Winit-self
flag in addition to -Wuninitialized
:
-Winit-self
(C, C++, Objective-C and Objective-C++ only) Warn about uninitialized variables which are initialized with themselves. Note this option can only be used with the-Wuninitialized
option, which in turn only works with-O1
and above.
My question is: Why is this not the default behavior for -Wuninitialized
? What is the use case where you want to warn about uninitialized variables, but not self-initialized ones, which are just as troublesome?
It looks like this bug report Warn about member variables initialized with itself has an explanation for this (emphasis mine):
I agree with Andrew, the a(a) mistake should always warn, it should be independent of -Winit-self, which exists so that -Wuninitialized doesn't warn about the common (but questionable) practice of self-initializing automatic variables to silence warnings.
It it probably called a questionable practice since it is undefined behavior in C++ to self initialize an automatic variable and the bug report is a C++ bug report.
这篇关于为什么-Winit-self与-Wuninitialized分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!