title: C++11 lambdas: member variable capture gotcha
body: <p>Consider this code:</p>
  
  <pre><code>#include &lt;memory&gt;
  #include &lt;iostream&gt;
  
  class A
  {
  public:
      A(int data) : data_(data)
      { std::cout &lt;&lt; "A(" &lt;&lt; data_ &lt;&lt; ")" &lt;&lt; std::endl; }
      ~A() { std::cout &lt;&lt; "~A()" &lt;&lt; std::endl; }
      void a() { std::cout &lt;&lt; data_ &lt;&lt; std::endl; }
  private:
      int data_;
  };
  
  class B
  {
  public:
      B(): a_(new A(13)) { std::cout &lt;&lt; "B()" &lt;&lt; std::endl; }
      ~B() { std::cout &lt;&lt; "~B()" &lt;&lt; std::endl; }
      std::function&lt;void()&gt; getf()
      {
          return [=]() { a_-&gt;a(); };
      }
  private:
      std::shared_ptr&lt;A&gt; a_;
  };
  
  int main()
  {
      std::function&lt;void()&gt; f;
      {
          B b;
          f = b.getf();
      }
      f();
      return 0;
  }
  </code></pre>
  
  <p>Here it looks like I'm capturing <code>a_</code> shared pointer by value, but when I run it on Linux (GCC 4.6.1), this is printed:</p>
  
  <pre><code>A(13)
  B()
  ~B()
  ~A()
  0
  </code></pre>
  
  <p>Obviously, 0 is wrong, because A is already destroyed. It looks like <code>this</code> is actually captured and is used to look up <code>this-&gt;a_</code>. My suspicion is confirmed when I change the capture list from <code>[=]</code> to <code>[=,a_]</code>. Then the correct output is printed and the lifetime of the objects is as expected:</p>
  
  <pre><code>A(13)
  B()
  ~B()
  13
  ~A()
  </code></pre>
  
  <p><strong>The question:</strong></p>
  
  <p>Is this behaviour specified by the standard, implementation-defined, or undefined? Or I'm crazy and it's something entirely different?</p>