https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69553
Bug ID: 69553 Summary: Optimizations O1/O2 makes std::array value incorrect when passed to function Product: gcc Version: 6.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: gee at ptilouk dot net Target Milestone: --- Created attachment 37521 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=37521&action=edit Source code generating the bug The following example displays incorrect values when compiling with optimizations -O1 or -O2 (but not without optimization and not with -O3): ---------------------- #include <iostream> #include <array> typedef std::array<std::array<double, 2>, 2> Matrix; void foo(const double &px, const double &py) { std::cerr << px << " " << py << std::endl; } std::ostream& operator<<(std::ostream& os, const std::array<double, 2>& p) { return os << p[0] << " " << p[1]; } void test (const Matrix& t) { std::cerr << "In test: " << std::endl << t[0] << std::endl << t[1] << std::endl; std::cerr << "In foo: " << std::endl; foo(t[0][0], t[0][1]); foo(t[1][0], t[1][1]); } int main (int, char**) { Matrix t = {{ {{1,2}}, {{3,4}} }}; test (t); return 0; } ---------------------- (Compiled with 'g++-6 -std=c++11 -O1 test_bug_array.cpp') Without optimization (or with O3), i have the following output: In test: 1 2 3 4 In foo: 1 2 3 4 Which makes sense. Now if I turn on O1 or O2, I get: In test: 1 2 3 4 In foo: 1 3 3 4 The bug does not appear when using boost::array or when replacing the structure by a simple C-array (double[2][2]) or by pairs (std::pair<std::pair<double> >).