title: Virtual functions and std::function?
body: <p>Consider the following code in C++17:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;functional&gt;
  
  struct base
  {
      base() {std::cout &lt;&lt; "base::base" &lt;&lt; std::endl;}
      virtual ~base() {std::cout &lt;&lt; "base::~base" &lt;&lt; std::endl;}
      virtual void operator()() {std::cout &lt;&lt; "base::operator()" &lt;&lt; std::endl;}
  };
  
  struct derived1: base
  {
      derived1() {std::cout &lt;&lt; "derived1::derived1" &lt;&lt; std::endl;}
      virtual ~derived1() {std::cout &lt;&lt; "derived1::~derived1" &lt;&lt; std::endl;}
      virtual void operator()() {std::cout &lt;&lt; "derived1::operator()" &lt;&lt; std::endl;}
  };
  
  struct derived2: base
  {
      derived2() {std::cout &lt;&lt; "derived2::derived2" &lt;&lt; std::endl;}
      virtual ~derived2() {std::cout &lt;&lt; "derived2::~derived2" &lt;&lt; std::endl;}
      virtual void operator()() {std::cout &lt;&lt; "derived2::operator()" &lt;&lt; std::endl;}
  };
  
  int main(int argc, char* argv[])
  {
      base* ptr1 = new derived1();
      base* ptr2 = new derived2();
      std::function f1(*ptr1);
      std::function f2(*ptr2);
      std::invoke(*ptr1);     // calls derived1::operator()
      std::invoke(*ptr2);     // calls derived2::operator()
      std::invoke(f1);        // calls base::operator()
      std::invoke(f2);        // calls base::operator()
      delete ptr1;
      delete ptr2;
      return 0;
  }
  </code></pre>
  
  <p><code>std::function</code> does not seem to do the right thing with virtual functions. Would there be any way to make <code>std::invoke(*ptrN)</code> and <code>std::invoke(fN)</code> behave the same way? Or would there be any way to create a new function wrapper that would deal with virtual functions?</p>