2

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?

3
  • rvalue/temporary cannot bind to non-const reference Commented May 25, 2013 at 14:45
  • 2
    Try adding an asterisk, like *[](int i){/*...*/}. No idea if that works... Commented May 25, 2013 at 14:53
  • @yngum, if this is the issue. I will expect the void (&&fptr)(int i) solve the problem. But it does not. @Kerrek, this actually works. Maybe this is because @Angew said, the closure object has conversion operator from closure=>pointer but not closure=>reference? Commented May 25, 2013 at 15:51

1 Answer 1

1

A lambda is not really a function, it's a closure object. Basically, a compiler-generated class with operator() defined. A non-capturing closure also defines a conversion operator for converting to good old pointer-to-function.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot! you seem correct. Can you tell me where can I find the document/source of this conversion operator of closure/function object?
@Wang The operator is required by C++11 standard, [expr.prim.lambda]§6. The almost-final draft of the standard can be obtained for free from open-std.org.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.