title: Passing a function object to std::function
body: <p>I'm having a little trouble using a function object create an instance of std::function. (My environment is C++ 11 using Visual Studio 2015.)</p>
  
  <p>First, I have a class that is used in the argument list of my function object. Here is a simplified version that illustrates the essential point.</p>
  
  <pre><code>class X
  {
  public:
      void zz(int a) const
      {
          std::cout &lt;&lt; "X:zz, a = " &lt;&lt; a &lt;&lt; "." &lt;&lt; std::endl;
      }
  };
  </code></pre>
  
  <p>Then here is the function object, which uses X in its operator() method.</p>
  
  <pre><code>class Y
  {
  public:
      void operator()(const X&amp; x)
      {
          x.zz(17);
      }
  };
  </code></pre>
  
  <p>Now, I'd like to wrap an instance of Y in a std::function. The type of the function is void(const X&amp;), a void function taking a const reference to X as an argument. The method Y:operator() has this signature.</p>
  
  <pre><code>void test_it
  {
      X my_x;
  
      Y my_y;
      std::function&lt;void(const X&amp;)&gt; f1(my_y);
      f1(my_x); // OK
  
      std::function&lt;void(const X&amp;)&gt; f2(Y());
      f2(my_x); // cannot convert argument 1 from X to Y
  
      std::function&lt;void(const X&amp;)&gt; f3(Y);
      f3(my_x); // cannot convert argument 1 from X to Y
  
      std::function&lt;void(const X&amp;)&gt; f4(Y{});
      f4(my_x); // OK
  }
  </code></pre>
  
  <p>The first example, f1, creates an instance of Y, my_y, and creates f1 using this instance. This works fine. (The contents of Y get moved into the std::function in the std::function constructor.)</p>
  
  <p>The second try, f2, constructs an instance of Y in the constructor for f2. When I try to invoke it with f2(my_x) the compiler complains that it cannot convert argument 1 (my_x) from an X to a Y.</p>
  
  <p>The third try, f3, where I just name the function object class in the std::function construction of f3 fails in the same way as f2.</p>
  
  <p>Finally, f4 works. In this one, I construct the function object Y using an empty initializer list.</p>
  
  <p>Perhaps there is a simple explanation, but I cannot see why two of these use-cases don't compile. In particular f2, using Y(), and f4 using Y{}, seem virtually identical to me.</p>