[Bug c++/70735] [5/6/7 Regression] problem combining std::function, generic lambdas and static variables

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Wed Apr 20 14:30:00 GMT 2016


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.


More information about the Gcc-bugs mailing list