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

--- Comment #11 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The local static variable seems to be captured by copy at the point when the
closure's call operator is instantiated, so it captures the current value of
the variable.

extern "C" int printf(const char*, ...);

int main()
{
  static int a;
  void(*f)(int) = [](auto) { printf("%d %p\n", a, (void*)&a); };
  a = 1;
  printf("%d %p\n", a, (void*)&a);
  f(0);
}

1 0x601044
0 0x601048

The library change (and the conversion to a function pointer in this example)
force the instantiation to happen earlier than it did with GCC 5.3, so that the
variable is copied when it equals 0 rather than at the expression f(0) when it
equals 1.

Reply via email to