http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49651
--- Comment #1 from Pedro Larroy <pedro.larroy at gmail dot com> 2011-07-05 20:30:13 UTC --- I think I'm able to reproduce with the following: the output spotted is: run.for_each.a: 20633 run.a: 20634 1 for_each.a: 20631 1 for_each.a: 20632 Compiled with: g++ -Wall -std=c++0x -O3 -o test test.cc Code follows: #include <iostream> #include <string> #include <cstdlib> #include <cassert> #include <vector> #include <stdexcept> #include <stdint.h> #include <algorithm> #include <stdint.h> typedef uint32_t u32; using namespace std; vector<u32> g; void f(u32 a, u32 b) { g.push_back(b); } int main(int argc, char *argv[]) { u32 a = 0; vector<int> vi; vi.push_back(0); vi.push_back(1); vi.push_back(2); vi.push_back(3); vi.push_back(4); vector<int> vo; vo.push_back(5); vo.push_back(5); vector<int> ve; ve.push_back(5); vector<int> v3; vector<int> v4; vector<int> v5; vector<int> v6; vector<int> v7; f(32, a++); cout << "bmain.a: " << a << endl; f(32, a++); cout << "bmain.a: " << a << endl; auto run = [&](int& i) { // inside this lambda &a is not the same as the &a in the first line v3.push_back(i); v4.push_back(i); v5.push_back(i); v6.push_back(i); v7.push_back(i); f(32, a++); cout << "run.a: " << a << endl; v3.push_back(i); v4.push_back(i); v5.push_back(i); v6.push_back(i); v7.push_back(i); f(32, a++); cout << "run.a: " << a << endl; for_each(ve.begin(), ve.end(), [&](int& xi) { v7.push_back(xi); f(32, a++); cout << "run.for_each.a: " << a << endl; }); f(32, a++); cout << "run.a: " << a << endl; }; f(32, a++); cout << "main.a: " << a << endl; while(true) { for_each(vi.begin(), vi.end(), [&](int& xi) { f(32, a++); cout << "1 for_each.a: " << a << endl; for_each(vo.begin(), vo.end(), run); f(32, a++); cout << "1 for_each.a: " << a << endl; for_each(ve.begin(), ve.end(), run); f(32, a++); cout << "1 for_each.a: " << a << endl; }); } cout << "main.a: " << a << endl; }