When using gcc 4.0.0 or 4.0.1 on MacOS, the following code fails when optimizations (-O1 - -O3) is turned on:
--- snip --- bool TestFoo(const bool& trueBool) { CFoo foo; // bool falseBool = trueBool ? false: true; // This works! bool falseBool = !trueBool; // This does not work! if (falseBool != foo.GetFalse()) { if (!falseBool) { // Should never be executed! foo.DoSomething(1); return true; } } return false; } int main (int /*argc*/, char * const /*argv[]*/) { bool trueBool = true; if (TestFoo(trueBool)) { return -1; // Error! } return 0; } --- snip --- The code works as intended when optimizations are turned off. It seems to be the ! operator not working with const bool&, but modifying the code in other ways may also solve the problem. (Try putting the CFoo class in the same module, or remove the inner if statement in TestFoo.) -- Summary: const bool& optimization error Product: gcc Version: 4.0.1 Status: UNCONFIRMED Severity: critical Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: fredrik dot littmarck at propellerheads dot se http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24737