-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutable_in_lambda.cpp
More file actions
26 lines (24 loc) · 851 Bytes
/
mutable_in_lambda.cpp
File metadata and controls
26 lines (24 loc) · 851 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Stackoverflow: https://stackoverflow.com/questions/5501959/why-does-c11s-lambda-require-mutable-keyword-for-capture-by-value-by-defau
// Answer:
// It requires mutable because by default, a function object should produce the same result every time it's called.
// This is the difference between an object orientated function and a function using a global variable,
// effectively.
#include <iostream>
int main()
{
int captureVar = 100;
auto functionObj = [captureVar]() mutable
{
captureVar += 1;
std::cout << "CaptureVar: " << captureVar << std::endl;
};
// SHOULD PRINT 101
functionObj();
// SHOULD PRINT 102
functionObj();
// SHOULD PRINT 103
functionObj();
// SHOULD PRINT 104
functionObj();
return 0;
}