Skip to content

Instantly share code, notes, and snippets.

@xkikeg
Created February 19, 2012 02:39
Show Gist options
  • Save xkikeg/1861693 to your computer and use it in GitHub Desktop.
Save xkikeg/1861693 to your computer and use it in GitHub Desktop.
C++ std::function<>におけるmemory allocationの疑問
#include <cassert>
#include <iostream>
#include <functional>
std::function<int (int)> make_g(int x)
{
std::cout << "Stack var: " << &x << std::endl;
return [x] (int y) {
std::cout << "Lambda var: " << &x << std::endl;
return x + y;
};
}
int main(int argc, char ** argv)
{
int x = (argc <= 1) ? 1 : std::atoi(argv[1]);
std::function<int (int)> g = make_g(x);
assert(x+5 == g(5));
auto h = [x](int y) {
std::cout << "unreturned var : " << &x << "\n" ;
return x + y;
};
assert(x+5 == h(5));
std::cout << "heap var: " << &std::vector<int>(5)[0] << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment