title: How to use a lambda expression as a template parameter?
body: <p>How to use lambda expression as a template parameter? E.g. as a comparison class initializing a std::set.</p>
  
  <p>The following solution should work, as lambda expression merely creates an anonymous struct, which should be appropriate as a template parameter. However, a lot of errors are spawned.</p>
  
  <p>Code example:</p>
  
  <pre><code>struct A {int x; int y;};
  std::set &lt;A, [](const A lhs, const A &amp;rhs) -&gt;bool {
      return lhs.x &lt; rhs.x;
      } &gt; SetOfA;
  </code></pre>
  
  <p>Error output (I am using g++ 4.5.1 compiler and --std=c++0x compilation flag):</p>
  
  <pre><code>error: ‘lhs’ cannot appear in a constant-expression
  error: ‘.’ cannot appear in a constant-expression
  error: ‘rhs’ cannot appear in a constant-expression
  error: ‘.’ cannot appear in a constant-expression
  At global scope:
  error: template argument 2 is invalid
  </code></pre>
  
  <p>Is that the expected behavior or a bug in GCC?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As someone pointed out, I'm using lambda expressions incorrectly as they return an <em>instance</em> of the anonymous struct they are referring to.</p>
  
  <p>However, fixing that error does not solve the problem. I get <code>lambda-expression in unevaluated context</code> error for the following code:</p>
  
  <pre><code>struct A {int x; int y;};
  typedef decltype ([](const A lhs, const A &amp;rhs) -&gt;bool {
      return lhs.x &lt; rhs.x;
      }) Comp;
  std::set &lt;A, Comp &gt; SetOfA;
  </code></pre>