In C++14/17, how do you access a lambda capture initialized variable outside the scope of the lambda?
Source:
#include <iostream>
using namespace std;
int main(){
auto test = [value1 =0]() mutable {value1+=1; return value1;};
cout << test() << endl;
cout << test() << endl;
//cout << value1 << endl;//error: ‘value1’ was not declared in this scope
}
Output:
1
2
Is the value1 variable accessible outside the scope of the test() lambda? What is the lifetime of a lambda capture initialized variable?
Attempting to access value1 outside the lambda gives the following error: ‘value1’ was not declared in this scope.
Compiled with gcc version 7.3.0 (Ubuntu 7.3.0-21ubuntu1~14.04).