This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]