This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/70735] [5/6/7 Regression] problem combining std::function, generic lambdas and static variables
- From: "redi at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Wed, 20 Apr 2016 14:29:47 +0000
- Subject: [Bug c++/70735] [5/6/7 Regression] problem combining std::function, generic lambdas and static variables
- Auto-submitted: auto-generated
- References: <bug-70735-4 at http dot gcc dot gnu dot org/bugzilla/>
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.