[英]C++ init-list: using non-initialized members to initialize others gives no warning
Neither g++ (4.4 and 4.6) nor clang++ (3.2) nor coverity, with -Wall and -Wextra (+ some others) or -Weverything respectively gives me a warning for the following code snippet: 既没有g ++(4.4和4.6)也没有clang ++(3.2),也没有覆盖,-Wall和-Wextra(+其他一些)或-Weverything分别给出了以下代码片段的警告:
class B {
char *t2;
char *t;
public:
B() : t2(t), t(new char[100]) {}
};
I would at least expect a small warning about the usage of uninitialized (member-) variables. 我至少会期待一个关于未初始化(成员)变量使用的小警告。
Is there something I'm missing? 有什么我想念的吗? Is this a wanted "no-warning"-scenario.
这是一个想要的“无警告” - 场景。 I have (now had) at least one bug in my software which was hard to find.
我(现在已经)我的软件中至少有一个很难找到的错误。
EDIT : As can be read in this new question I realized that coverity warns about this problem in some cases. 编辑 :从这个新问题可以看出,我意识到在某些情况下, 封面警告这个问题。
There is no good reason not to issue a warning here. 没有充分的理由不在这里发出警告。
G++ isn't smart enough to diagnose unintialized members in constructors, see http://gcc.gnu.org/PR2972 G ++不够智能,无法诊断构造函数中的非实体成员,请参阅http://gcc.gnu.org/PR2972
I have a work-in-progress patch to fix it which I hope to finish "some time this year" 我有一个正在进行中的补丁修复它,我希望“今年某个时候”完成
Even with my patch I'm not sure G++ would warn, because t2
is initialized, but it's initialized to an indeterminate value. 即使使用我的补丁,我也不确定G ++是否会发出警告,因为
t2
已初始化,但它已被初始化为不确定的值。 For the compiler to track that is not trivial, but should be possible (so I'm surprised even Coverity misses it.) Run-time tools such as valgrind
get it right though. 对于编译器来说,跟踪这不是一件容易的事情,但应该是可能的(所以即使Coverity错过了我也很惊讶。)像
valgrind
这样的运行时工具可以解决它。
When I revisit my patch I'll consider this case and see whether I can make it warn, without adding too much overhead (currently my patch checks whether members without an initializer would leave data uninitialized, to catch this I would need to also check members with an initializer and check whether that initializer relies on another member which isn't yet initialized, which would need to be checked for every member, which might have an effect on compilation speed for classes with lots of members.) 当我重新访问我的补丁时,我会考虑这种情况,看看我是否可以发出警告,而不会增加太多开销(目前我的补丁检查没有初始化程序的成员是否会保留数据未初始化,要抓住这个我还需要检查成员使用初始化程序并检查初始化程序是否依赖于另一个尚未初始化的成员,这需要为每个成员检查,这可能会对具有大量成员的类的编译速度产生影响。)
The C++ standard says that using uninitialized variables leads to undefined behaviour. C ++标准说使用未初始化的变量会导致未定义的行为。 It does not mandate that the compiler issue a diagnostic about it.
它并不要求编译器对其发出诊断。 So getting a warning or not is a QOI (Quality of Implementation) thing.
所以得到警告与否是QOI(实施质量)的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.