https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113111
Bug ID: 113111 Summary: -Werror=uninitialized is not consistent for optimization level 0 or -std=before-c++20 Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: MikeSmith32564 at mail dot com Target Milestone: --- The following snippet: #include <string> #include <iostream> struct Dummy { std::string val; }; int main() { Dummy d = { d.val = "random text #########################################################" }; std::cout<<d.val<<"\n"; } when built with: g++ -std=c++20 -O0 -Wall -Werror main.cpp -o a.out should generate a warning a.k.a. compilation error due to -Werror about trying to assign from an uninitialized variable. When -O1, -O2 or -O3 are used the expected warning is shown: include/c++/14.0.0/bits/basic_string.h:1084:16: error: 'd.Dummy::val.std::__cxx11::basic_string<char>::_M_string_length' is used uninitialized [-Werror=uninitialized] 1084 | { return _M_string_length; } | ^~~~~~~~~~~~~~~~ The example triggers undefined behavior when executed of course due to running std::string::operator= with the this pointer pointing to uninitialized memory but I believe a warning should be raised when building regardless of the optimization level selected. Strangely, standards before c++20 also do not report the warning.