title: How to declare reference to lambda in C++
body: <p>I want to declare a function that takes as argument a function (or lambda) with a specific prototype.</p>
  
  <p>The first try is:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  int test(int (&amp;val)(int)) {
      return val(2);
  }
  int main() {
      cout &lt;&lt; test([](int v){
          return v+100;
      });
      return 0;
  }
  </code></pre>
  
  <p>witch results in <code>error: invalid initialization of non-const reference of type 'int (&amp;)(int)' from an rvalue of type 'main()::&lt;lambda(int)&gt;'</code></p>
  
  <p>I tried to add const specified to the type but I don't know where exactly so I tried the following that works with GCC (-std=c++14), however I suspect it is illegal since it fails with clang:</p>
  
  <pre><code>int test(const auto&amp; val) {
      return val(2);
  }
  </code></pre>
  
  <p>I know I can use template or function pointers or std::function to achieve the same, then please consider this a didactic question. I want to know what does GCC deduce the type of val to, in the second example above (<code>int test(const auto&amp; val)</code>).</p>
  
  <pre><code>template &lt;typename F&gt;
  int test(F val) {
      return val(2);
  }
  
  int test(int val(int)) {
      return val(2);
  }
  
  int test(std::function&lt;int(int)&gt; val) {
      return val(2);
  }
  </code></pre>