The following code does not compile:
typedef void(*RunnableFun)(int); //pointer-to-function type
void foo(RunnableFun f) {
}
void bar(const std::string& a) {
foo([&](int) -> void { std::cout << a; });
}
and IntelliSense is telling me
no suitable conversion function from "lambda []void (int)->void" to "RunnableFun" exists
and the compiler is complaining
'void foo(RunnableFun)' : cannot convert argument 1 from 'bar::<lambda_796873cf40a6be4e411eb9df14f486bf>' to 'RunnableFun'
But the following does compile:
typedef void(*RunnableFun)(int); //pointer-to-function type
void foo(RunnableFun f) {
}
void bar(const std::string&) {
// Notice the empty capture list
foo([](int) -> void { std::cout << "dummy"; });
}
How can I keep the signature of foo() but achieve what I tried in to in the first code example?
P.S.: Changing foo's signature to void foo(std::function<void(int)> f) would compile but can I do it without changing it?