title: Avoid memory allocation with std::function and member function
body: <p>This code is just for illustrating the question.</p>
  
  <pre><code>#include &lt;functional&gt;
  struct MyCallBack {
      void Fire() {
      }
  };
  
  int main()
  {
      MyCallBack cb;
      std::function&lt;void(void)&gt; func = std::bind(&amp;MyCallBack::Fire, &amp;cb);
  }
  </code></pre>
  
  <p>Experiments with valgrind shows that the line assigning to <code>func</code> dynamically allocates about 24 bytes with gcc 7.1.1 on linux.</p>
  
  <p>In the real code, I have a few handfuls of different structs all with a <code>void(void)</code> member function that gets stored in ~10 million  <code>std::function&lt;void(void)&gt;</code>.</p>
  
  <p>Is there any way I can avoid memory being dynamically allocated when doing <code>std::function&lt;void(void)&gt; func = std::bind(&amp;MyCallBack::Fire, &amp;cb);</code> ? (Or otherwise assigning these member function to a <code>std::function</code>)</p>