title: C++11 std::function and std::reference wrapper used for sorting std::set
body: <p>I have a C++ class, and one of its fields is a std::set of objects. I want to write my own comparison function, or let the user specify one. In C++11 there's a new way to handle generic function types: std::function. It works with function pointers, member function pointers, lambda functions, etc.</p>
  
  <p>I tried to write a simple experiment program but it keeps craching all the time, even when I do what the C++11 Wikipedia article suggets. Maybe I just don't get how std::function and std::ref are supposed to be used.</p>
  
  <p>Anyway, the point is that when I created a std::function from a simple lambda function and made it a class member, the sizeof of the class grew by 22. When I created a std::function from a pointer to a global function, this std::function's sizeof was 32. So the size is big. I'm going to have many objects using the same comparison function, so I prefer to have one function used by all of them.</p>
  
  <p>I have two ideas, tell me what you think. One idea, use std::ref to store a reference to a function, this way I can define one function and many objects will use it to compare the std::set elements. Second idea: if it doesn't work like that, or the resulting function object is too big anyway, maybe I can use a shared_ptr.</p>
  
  <p>You may wonder: why not have one static std::function member? The answer: because then ALL objects will use the same comparison function. I want to be able to have, for example, 1000 objects, with 400 using one comparison function and 600 using a different comparison function.</p>
  
  <p>Example:</p>
  
  <pre><code>class MyClass
  {
  public:
  private:
       std::function&lt;bool (int, int)&gt; compare;
       std::set&lt;int&gt; set;
  };
  </code></pre>
  
  <p>Now how do I make the std::set use the std::function, and have many MyClass objects use the same function?</p>
  
  <p>I'd like to be able to change the comparison function during run-time, so that the user would be able to choose the ordering of the objects in the set (which are displayed by GUI).</p>