https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89257

            Bug ID: 89257
           Summary: Bad optimisation at -O3 with vector of pairs
           Product: gcc
           Version: 7.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: matt at ookypooky dot com
  Target Milestone: ---

Code as follows:
--
#include <vector>
#include <cstdio>

struct Foo
{
    Foo () = default;

    Foo (Foo &&f)
      : x (f.x)
      , y (f.y)
    {
      f.y = 0;
    }

    int x = 123;
    int y = 456;
};

int main ()
{
  std::vector<std::pair<Foo, Foo>> v;
  for (size_t i = 0; i < 10; ++i)
  {
    v.emplace_back ();
  }

  for (const auto &p : v)
  {
    printf ("%d\n", p.first.y);
  }
}
--

When compiled with:

g++ -std=c++17 -O3 anyway.cpp 

Prints:

--
0
0
0
0
0
0
0
0
456
456
--

I expect every line to print 456. 

Works correctly at -O2, and at -std=c++14, and works with std::tuple in place
of std::pair. Also works on gcc-6.3.0.

I'm afraid I don't have newer compiler versions to test against (and Compiler
Explorer output is hard to interpret without getting rid of std::vector).

Reply via email to