Skip to content

Instantly share code, notes, and snippets.

@PIlin
Created January 20, 2013 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PIlin/4580208 to your computer and use it in GitHub Desktop.
Save PIlin/4580208 to your computer and use it in GitHub Desktop.
// g++ -g -Wall -std=c++11 1.cpp
#include <iostream>
#include <vector>
#include <functional>
typedef std::vector<std::function<int(int)> > VFII;
VFII gen()
{
VFII v;
for (int i = 0; i < 10; ++i)
{
v.push_back([=](int n) { return i + n; });
// v.push_back([&](int n) { return i + n; }); // <<<< UB
}
return v;
}
int main()
{
auto v = gen();
for (auto f : v)
{
std::cout << f(4) << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment