Skip to content

Instantly share code, notes, and snippets.

@lessandro
Last active December 20, 2015 14:29
Show Gist options
  • Save lessandro/6147373 to your computer and use it in GitHub Desktop.
Save lessandro/6147373 to your computer and use it in GitHub Desktop.
#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;
}
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