#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
// Block 1
{
auto inc = []() { int i = 0; return [&]() { return i++; }; }();
std::vector<int> v(10, 10);
std::generate(v.begin(), v.end(), inc);
for (auto i : v) std::cout << i << std::endl;
}
// Block 2
{
auto inc = []() { int i = 0; return [&]() { return i++; }; };
std::vector<int> v(10, 10);
std::generate(v.begin(), v.end(), inc());
for (auto i : v) std::cout << i << std::endl;
}
}
I am not sure why these two blocks produce different results.
Block 1: 32767 ... 32776
Block 2: 0 ... 10
And for std::generate the generator(inc) is passed by value, so I believe it should be fine, right?
I am running OS X.
Thanks,
Note that the results from above code are undefined, see below.