Order of execution for lambda snippet not understood...

I found the following code and output in the textbook:
"The C++ standard library".

Why is the assignment to 42 not carried out until after the lambda is executed?

1
2
3
4
5
6
7
8
9
10
11
int id = 0;
auto f = [id] () mutable {
std::cout << "id: " << id << std::endl;
++id;
};

id = 42;
f();
f();
f();
std::cout << id << std::endl;


Indicated output:
id: 0
id: 1
id: 2
42

???
See https://msdn.microsoft.com/en-us/library/dd293608.aspx

The id used by f() is a copy by value and the copy was created already on line 2.
Topic archived. No new replies allowed.