Passing the lambda as function pointer works fine with gcc 4.6.3:
#example adapt from LoudNPossiblyWrong http://stackoverflow.com/questions/3351280/c0x-lambda-to-function-pointer-in-vs-2010
#include <iostream>
using namespace std;
void func(int i){cout << "I'V BEEN CALLED: " << i <<endl;}
void fptrfunc(void (*fptr)(int i), int j){fptr(j);}
int main(){
fptrfunc(func,10); //this is ok
fptrfunc([](int i){cout << "LAMBDA CALL " << i << endl; }, 20); //works fine
return 0;
}
However passing the lambda as reference will not work:
#example adapt from LoudNPossiblyWrong http://stackoverflow.com/questions/3351280/c0x-lambda-to-function-pointer-in-vs-2010
#include <iostream>
using namespace std;
void func(int i){cout << "I'V BEEN CALLED: " << i <<endl;}
void freffunc(void (&fptr)(int i), int j){fptr(j);}
int main(){
freffunc(func,10); //this is ok
freffunc([](int i){cout << "LAMBDA CALL " << i << endl; }, 20); //DOES NOT COMPILE
return 0;
}
error: invalid initialization of non-const reference of type ‘void (&)(int)’
from an rvalue of type ‘<lambda(int)>’
Can anyone explain why is that?