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