https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90313
Bug ID: 90313 Summary: Is an assignment elided with gcc7.3 -O2? Product: gcc Version: 7.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: nina.korshunova at tum dot de Target Milestone: --- #include <array> #include <iostream> using Coordinates = std::array<double, 3>; Coordinates map( const Coordinates& c, size_t level ) { Coordinates result{ c[1], c[2], c[0] }; if( level != 0 ) { result = map( result, level - 1 ); } return result; } int main( ) { Coordinates vecOfCoordinates = { 1.0, 2.0, 3.0 }; size_t level = 1; auto result = map( vecOfCoordinates, level ); std::cout << "result = (" << result[0] << ", " << result[1] << ", " << result[2] << "), should be (3, 1, 2)" << std::endl; return 0; } This minimal example delivers the following results with different optimizations: g++-6 -O2 : result = (3, 1, 2), should be (3, 1, 2) g++-7: result = (3, 1, 2), should be (3, 1, 2) g++-7 -O2: result = (3, 1, 3), should be (3, 1, 2) g++-7 -O2 -fno-elide-constructors: result = (3, 1, 2), should be (3, 1, 2) g++-8 -O2: result = (3, 1, 2), should be (3, 1, 2) Gcc7.3.0 and -O2 deliver the wrong result. -fno-elide-constructors fixes the result. -Wall -Wextra -fno-strict-aliasing -fwrap did not make a difference. Is this a possible bug, or is there a problem in the code? Everything is run on Ubuntu 18.04.