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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2011-12-15 00:00:00         |2018-5-30

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Comment 3 may be a different issue. Clang elides the copy for the original
report with the dummy scope, but doesn't for comment 3.

extern "C" int puts(const char*);

struct A {
  int i;
  A(int i) : i(i) { puts("cons"); }
  A(A const &) { puts("copy"); }
};

A f(bool b)
{
  puts("f(bool)");
  if (b) return A(0);
  A a(1);
  return a;
}

A g()
{
  puts("g()");
  if (false) { }
  A a(1);
  return a;
}

A h()
{
  puts("h()");
  {
    A a(0);
    return a;
  }
}

int main()
{
  A a = f(false);
  A b = g();
  A c = h();
  return a.i - b.i + c.i;
}

For this code GCC prints:

f(bool)
cons
copy
g()
cons
h()
cons
copy

But Clang prints:

f(bool)
cons
copy
g()
cons
h()
cons

(Neither result is affected by optimization)

Reply via email to