std::functions and lambda function passing
<p>I have a class that takes a <code>std::function</code> as a parameter which I assign a lambda function. It works in the constructor but it stops working after that. The debugger says <code>f</code> is "empty" after running the first line. Why?</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;functional&gt;

typedef std::function&lt;void(std::string)&gt; const&amp; fn;

class TestClass
{
public:
    TestClass(fn _f) : f(_f) { F(); }
    void F() { f("hello"); };

private:
    fn f;
};


int main()
{
    TestClass t([](std::string str) {std::cout &lt;&lt; str &lt;&lt; std::endl; });

    t.F();

    return 0;
}
</code></pre>

<p>Calling <code>t.F()</code> causes a fault. Why?</p>

<p>I can solve it by changing it to the following:</p>

<pre><code>int main()
{
    fn __f = [](std::string str) {std::cout &lt;&lt; str &lt;&lt; std::endl; };
    TestClass t(__f);

    t.F();

    return 0;
}
</code></pre>

<p>but again, this does not work when I change <code>fn</code> to <code>auto</code>!</p>

<pre><code>int main()
{
    auto __f = [](std::string str) {std::cout &lt;&lt; str &lt;&lt; std::endl; };
    TestClass t(__f);

    t.F();

    return 0;
}
</code></pre>

<p>What is the explanation of why this is happening?</p>