title: Creating std::function with lambda causes superfluous copying of the lambda object - why?
body: <p>When I am constructing std::function with lambda with captured values it makes an additional copy (move) of those parameters (actually the of the whole lambda object I guess).
  The code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;functional&gt;
  
  // Testing class - just to see constructing/destructing.
  class T {
  private:
      static int idCounter; // The global counter of the constructed objects of this type.
  public:
      const int id; // Unique object ID 
  
      inline T() : id(++idCounter) { 
          std::cout &lt;&lt; "  Constuctor Id=" &lt;&lt; id &lt;&lt; std::endl;
      };
      inline T(const T&amp; src) : id(++idCounter) {
          std::cout &lt;&lt; "  Copy constructor Id=" &lt;&lt; id &lt;&lt; std::endl;
      }
      inline T(const T&amp;&amp; src) : id(++idCounter) {
          std::cout &lt;&lt; "  Move constructor Id=" &lt;&lt; id  &lt;&lt; std::endl;
      }
      inline void print() const {
          std::cout &lt;&lt; "  Print is called for object with id=" &lt;&lt; id &lt;&lt; std::endl;
      }
      inline ~T() {
          std::cout &lt;&lt; "  Destructor Id=" &lt;&lt; id &lt;&lt; std::endl;
      }
  };
  
  int T::idCounter=0; 
  
  // Declare type of the std::function to store our lambda.
  typedef std::function&lt;int (void)&gt; Callback;
  
  int main()
  { 
      std::cout &lt;&lt; "Let's the game begin!" &lt;&lt; std::endl;
      T obj; // Custruct the first object.
      std::cout &lt;&lt; "Let's create a pointer to the lambda." &lt;&lt; std::endl;
      // Make a labmda with captured object. (The labmda prints and returns object's id).
      // It should make one (local) copy of the captured object but it makes it twice - why?!
      const Callback* pcb= new Callback( [obj]() -&gt; int { 
          obj.print();
          return obj.id; 
      } );
      std::cout &lt;&lt; "Now let's print lambda execution result."  &lt;&lt; std::endl;
      std::cout &lt;&lt; "The functor's id is " &lt;&lt; (*pcb)() &lt;&lt; std::endl;
      std::cout &lt;&lt; "Destroying the lambda." &lt;&lt; std::endl;
      delete pcb;
      std::cout &lt;&lt; "Terminating." &lt;&lt; std::endl;
      return 0;
  
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <blockquote>
    <p><PRE>
    Let's the game begin!
     Constuctor Id=1
    Let's create a pointer to the lambda.
     Copy constructor Id=2
    <b> Move constructor Id=3</b>
    Destructor Id=2
     Now let's print lambda execution result.
     Print is called for object with id=3
     The functor's id is 3
    Destroying the lambda.
     Destructor Id=3
    Terminating.
     Destructor Id=1
    </PRE></p>
  </blockquote>
  
  <p>I made a std:function with lambda with captured object. It should make a local copy of the object for lambda but it make the copy twice (look at move constructor call - highlighted with bold). Actually it make a copy of the whole lambda object. Why? How can I avoid that?
  I am using lambdas for inter-thread event processing and they may capture noticeable amounts of date so I am trying to find a way to avoid unnecessary copying. So the task is simple - to pass constructed lambda into the function with minimal expenses - if it will copy data twice for every constructed lambda I would search for another way to work with events.
  <BR/>I am using GCC v4.7.2 forced to GNU C++11.</p>