https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63203
Bug ID: 63203 Summary: Self-initialization of reference not diagnosed if it occurs within a loop Product: gcc Version: 4.9.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: jzwinck at gmail dot com The following is nonsensical yet compiles cleanly with "g++ -Wall -Wextra -Werror -Winit-self" (I tested GCC 4.7.2 and 4.9.0): #include <iostream> #include <string> int main() { for (int ii = 0; ii < 1; ++ii) { const std::string& str = str; // !! std::cout << str << std::endl; } } The line marked !! results in undefined behavior, yet is not diagnosed by GCC. However, commenting out the for line makes GCC complain: error: ‘str’ is used uninitialized in this function [-Werror=uninitialized] If you change std::string to int and turn on optimization, GCC will diagnose the error even with the loop. Ref: http://stackoverflow.com/questions/25717740/why-is-gcc-tricked-into-allowing-undefined-behavior-simply-by-putting-it-in-a-lo