https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78522
Bug ID: 78522 Summary: -O2 optimization confused by enum and pointer usage in constructors. Product: gcc Version: 4.8.4 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: huangs at chromium dot org Target Milestone: --- Created attachment 40145 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=40145&action=edit The source file and makefile. g++ -O2 and -O3 can produce results different from unoptimized and -O1, under a mixture of: - enum usage - pointer usage- - class initialization Bug disappears when attempting to debug using fprintf(). Source and Makefile are attached, but for convenience here's the code: ======== #include <cstdint> #include <cstdio> enum ValueType : uint32_t { GOOD_VALUE = 0, BAD_VALUE }; class Helper { public: Helper() { flag = true; } void run(uint32_t* value) { if (flag) { *value = GOOD_VALUE; } } bool flag; }; class Runner { public: Runner() : value_type(BAD_VALUE) { Helper helper; helper.run(reinterpret_cast<uint32_t*>(&value_type)); if (value_type == GOOD_VALUE) { fprintf(stderr, "Good!\n"); // Expected, works if non-optimized. } else { fprintf(stderr, "Bad!\n"); // Unexpected, happens for -O2 and -O3. } } // If we make this uint32_t, or make it local variable then bug disappears! ValueType value_type; }; int main() { Runner runner; return 0; } ========