title: C++11 std::set lambda comparison function
body: <p>I want to create a <code>std::set</code> with a custom comparison function. I could define it as a class with <code>operator()</code>, but I wanted to enjoy the ability to define a lambda where it is used, so I decided to define the lambda function in the initialization list of the constructor of the class which has the <code>std::set</code> as a member. But I can't get the type of the lambda. Before I proceed, here's an example:</p>
  
  <pre><code>class Foo
  {
  private:
       std::set&lt;int, /*???*/&gt; numbers;
  public:
       Foo () : numbers ([](int x, int y)
                         {
                             return x &lt; y;
                         })
       {
       }
  };
  </code></pre>
  
  <p>I found two solutions after searching: one, using <code>std::function</code>. Just have the set comparison function type be <code>std::function&lt;bool (int, int)&gt;</code> and pass the lambda exactly like I did. The second solution is to write a make_set function, like <code>std::make_pair</code>.</p>
  
  <p>SOLUTION 1:</p>
  
  <pre><code>class Foo
  {
  private:
       std::set&lt;int, std::function&lt;bool (int, int)&gt; numbers;
  public:
       Foo () : numbers ([](int x, int y)
                         {
                             return x &lt; y;
                         })
       {
       }
  };
  </code></pre>
  
  <p>SOLUTION 2:</p>
  
  <pre><code>template &lt;class Key, class Compare&gt;
  std::set&lt;Key, Compare&gt; make_set (Compare compare)
  {
       return std::set&lt;Key, Compare&gt; (compare);
  }
  </code></pre>
  
  <p>The question is, do I have a good reason to prefer one solution over the other? I prefer the first one because it makes use of standard features (make_set is not a standard function), but I wonder: does using <code>std::function</code> make the code (potentially) slower? I mean, does it lower the chance the compiler inlines the comparison function, or it should be smart enough to behave exactly the same like it would it was a lambda function type and not <code>std::function</code> (I know, in this case it can't be a lambda type, but you know, I'm asking in general) ?</p>
  
  <p>(I use GCC, but I'd like to know what popular compilers do in general)</p>
  
  <p><strong>SUMMARY, AFTER I GOT LOTS OF GREAT ANSWERS:</strong></p>
  
  <p>If speed is critical, the best solution is to use an class with <code>operator()</code> aka functor. It's easiest for the compiler to optimize and avoid any indirections.</p>
  
  <p>For easy maintenance and a better general-purpose solution, using C++11 features, use <code>std::function</code>. It's still fast (just a little bit slower than the functor, but it may be negligible) and you can use any function - <code>std::function</code>, lambda, any callable object.</p>
  
  <p>There's also an option to use a function pointer, but if there's no speed issue I think <code>std::function</code> is better (if you use C++11).</p>
  
  <p>There's an option to define the lambda function somewhere else, but then you gain nothing from the comparison function being a lambda expression, since you could as well make it a class with <code>operator()</code> and the location of definition wouldn't be the set construction anyway.</p>
  
  <p>There are more ideas, such as using delegation. If you want a more thorough explanation of all solutions, read the answers :)</p>