title: C++ lambda expressions - How does the compiler interpret them?
body: <p>I just starter learning the new features in C++ 11. I was reading about lambdas in C++ Primer (Stanley Lippman) and was experimenting with them.</p>
  
  <p>I tried the following pieces of code:</p>
  
  <pre><code>auto func() -&gt; int (*) (){
      //int c=0;
      return []()-&gt; int {return 0;};
  }
  
  int main(){
      auto p = func();
  }
  </code></pre>
  
  <p>This code compiled fine. So I guess lambdas without any captures are just generated as normal functions by the compiler and we can use a normal function pointer to them.</p>
  
  <p>Now I changed the code to use captures:</p>
  
  <pre><code>auto func() -&gt; int (*) (){
      int c=0;
      return [=]()-&gt; int {return c;};
  }
  
  int main(){
      auto p = func();
  }
  </code></pre>
  
  <p>But this failed to compile. I got the following compilation error while using g++:</p>
  
  <pre><code>main.cpp: In function ‘int (* func())()’:
  main.cpp:6:31: error: cannot convert ‘func()::__lambda0’ to ‘int (*)()’ in return
  return [=]()-&gt; int {return c;};
  </code></pre>
  
  <p>From the error I can understand that it is not a normal function that is generated and it might probably be a class with an overloaded call-operator. Or is it something else?</p>
  
  <p>My questions : How does the compiler handle lambdas internally? How should I pass around lambdas that use captures i.e. what should be the return value from func()? I cant currently think of a use case where I would need to use lambdas like this but I just want to understand more about them. Please help.</p>
  
  <p>Thanks.</p>