Last active
December 20, 2015 14:29
-
-
Save lessandro/6147373 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <functional> | |
#include <cstdio> | |
std::function<void (void)> func; | |
struct A | |
{ | |
A(int n_) : n(n_) | |
{ | |
printf("%p A()\n", this); | |
} | |
A(const A &a) : n(a.n) | |
{ | |
printf("%p A(A &a) %p\n", this, &a); | |
} | |
A(const A &&a) : n(a.n) | |
{ | |
printf("%p A(A &&a) %p\n", this, &a); | |
} | |
~A() | |
{ | |
printf("%p ~A()\n", this); | |
} | |
int n; | |
}; | |
void f(const A &a) | |
{ | |
printf("hi %d! @ %p\n", a.n, &a); | |
} | |
void setup(int n) | |
{ | |
A a(n); | |
f(a); | |
func = [=]{ f(a); }; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
setup(10); | |
puts("func()"); | |
func(); | |
setup(20); | |
func(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clang++ -std=c++11 -stdlib=libc++ f.cpp -O3 | |
0x7fff506d1930 A() | |
hi 10! @ 0x7fff506d1930 | |
0x7fff506d1928 A(A &a) 0x7fff506d1930 | |
0x7fff506d1938 A(A &&a) 0x7fff506d1928 | |
0x7fe608c000e8 A(A &&a) 0x7fff506d1938 | |
0x7fff506d1938 ~A() | |
0x7fff506d1928 ~A() | |
0x7fff506d1930 ~A() | |
func() | |
hi 10! @ 0x7fe608c000e8 | |
0x7fff506d19a0 A() | |
hi 20! @ 0x7fff506d19a0 | |
0x7fff506d19a0 ~A() | |
hi 10! @ 0x7fe608c000e8 | |
0x7fe608c000e8 ~A() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment