When is 'this' captured in a lambda? : <p>I have a function in a class that defines a lambda and stores it in a local static variable:</p>

<pre><code>class A
{
public:
    void call_print()
    {
        static auto const print_func = [this] {
            print();
        };

        print_func();
    };

    virtual void print()
    {
        std::cout &lt;&lt; "A::print()\n";
    }
};

class B : public A
{
public:
    virtual void print() override
    {
        std::cout &lt;&lt; "B::print()\n";
    }
};
</code></pre>

<p>I also execute the following test:</p>

<pre><code>int main()
{
    A a;
    B b;

    a.call_print();
    b.call_print();
}
</code></pre>

<p>(<a href="http://coliru.stacked-crooked.com/a/2fe9b4702bb01d02">Live Sample</a>)</p>

<p>What I expect to be printed is:</p>

<pre><code>A::print()
B::print()
</code></pre>

<p>But what I really get is:</p>

<pre><code>A::print()
A::print()
</code></pre>

<p>(Same object address is also printed with each)</p>

<p>I suspect this is due to the <code>this</code> capture. I assumed that it would capture the value of <code>this</code> when it is called, however it seems to be captured the moment the lambda is defined.</p>

<p>Could someone explain the semantics of lambda captures? When do they actually get provided to the function? Is it the same for all capture types, or is <code>this</code> a special case? Removing <code>static</code> fixes the problem, however in my production code I'm actually storing the lambda in a slightly-heavier object which represents a slot to which I insert into a signal later.</p>
