2

I have not managed to find why this code does not work:

#include <iostream> #include <functional> using namespace std; int main() { auto xClosure = [](const function<void(int&)>& myFunction) { myFunction(10);}; xClosure([] (int& number) -> void {cout<<number<<endl; }); return 0; } 

It returns:

g++ test.cc -o test -std=c++14 
 test.cc:9:5: error: no matching function for call to object of type 'const function<void (int &)>' 
0

1 Answer 1

9

This has nothing to do with lambdas:

void test(const function<void(int&)>& myFunction) { myFunction(10); } 

this fails to compile for the same reason; you cannot bind the literal 10 to an int&.

Maybe you meant

const function<void(int)>& myFunction 

doing so and also modifying the signature of your lambda should make your code compile.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.