title: Should I copy an std::function or can I always take a reference to it?
body: <p>In my C++ application (using Visual Studio 2010), I need to store an std::function, like this:</p>
  
  <pre><code>class MyClass
     {
     public:
        typedef std::function&lt;int(int)&gt; MyFunction;
        MyClass (Myfunction &amp;myFunction);
     private:
        MyFunction m_myFunction;    // Should I use this one?
        MyFunction &amp;m_myFunction;   // Or should I use this one?
     };
  </code></pre>
  
  <p>As you can see, I added the function argument as a reference in the constructor.</p>
  
  <p>But, what is the best way to store the function in my class?</p>
  
  <ul>
  <li>Can I store the function as a reference since std::function is just a function-pointer and the 'executable code' of the function is guaranteed to stay in memory?</li>
  <li>Do I have to make a copy in case a lambda is passed and the caller returns?</li>
  </ul>
  
  <p>My gut feeling says that it's safe to store a reference (even a const-reference).  I expect the compiler to generate code for the lambda at compile time, and keep this executable code in 'virtual' memory while the application is running.  Therefore the executable code is never 'deleted' and I can safely store a reference to it.  But is this really true?</p>