Can a lambda expression be passed as function pointer?

LoudNPossiblyWrong picture LoudNPossiblyWrong · Jun 7, 2010 · Viewed 12k times · Source

I am trying to pass a lambda expression to a function that takes a function pointer, is this even possible?

Here is some sample code, I'm using VS2010:

#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); //DOES NOT COMPILE
    return 0;
}

Answer

Terry Mahaffey picture Terry Mahaffey · Jun 7, 2010

In VC10 RTM, no - but after the lambda feature in VC10 was finalized, the standard committee did add language which allows stateless lambdas to degrade to function pointers. So in the future this will be possible.