title: Move constructor called twice when move-constructing a std::function from a lambda that has by-value captures
body: <p>When move-constructing a <code>std::function</code> object from a <em>lambda</em>, where that lambda has by-value captures, it appears that the move-constructor of the object that is <em>value-captured</em> is called twice. Consider</p>
  <pre>#include &lt;functional&gt;
  #include &lt;iostream&gt;
  
  struct Foo
  {
      int value = 1;
  
      Foo() = default;
  
      Foo(const Foo &amp;) {}
  
      Foo(Foo &amp;&amp;)
      {
          std::cout &lt;&lt; &quot;move ctor&quot; &lt;&lt; std::endl;
      }
  };
  
  int main()
  {
      Foo foo;
      auto lambda = [=]() { return foo.value; };
      std::cout &lt;&lt; &quot;---------&quot; &lt;&lt;  std::endl;
      std::function&lt;int()&gt; func(std::move(lambda));
      std::cout &lt;&lt; &quot;---------&quot; &lt;&lt;  std::endl;
      return 0;
  }
  </pre>
  <p>The output is</p>
  <pre><code>---------
  move ctor
  move ctor
  ---------
  </code></pre>
  <p>I work on Mac OS X Catalina and my compiler is</p>
  <pre><code>g++-9 (Homebrew GCC 9.3.0) 9.3.0
  </code></pre>
  <p>I compile with <code>g++ -std=c++17</code>.</p>
  <p>I guess this behavior might be somewhat compiler-implementation-dependent, but I am still curious about the mechanism.</p>
  <p>Can someone please explain why the move constructor was called twice and what really happened there?</p>