Why is -Winit-self separate from -Wuninitialized

孤街浪徒 提交于 2020-01-14 12:38:02

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/22965414/why-is-winit-self-separate-from-wuninitialized

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!