I have the following code (simplified):
#include <functional>
template <typename... Args> void Callback(std::function<void(Args...)> f){
// store f and call later
}
int main(){
Callback<int, float>([](int a, float b){
// do something
});
}
The goal of this is to take some extra parameters, send off a packet, handle the response and call the lambda with the results The issue with this is, it doesnt take the lambda.
# g++ -std=c++11 test.cpp
test.cpp: In function ‘int main()’:
test.cpp:8:3: error: no matching function for call to ‘Callback(main()::<lambda(int, float)>)’
test.cpp:8:3: note: candidate is:
test.cpp:2:34: note: template<class ... Args> void Callback(std::function<void(Args ...)>)
test.cpp:2:34: note: template argument deduction/substitution failed:
test.cpp:8:3: note: ‘main()::<lambda(int, float)>’ is not derived from ‘std::function<void(Args ...)>’
Is there any way to get this to work without going through the hassle of wrapping the lambda explicitly in a std::function?
Callback(std::function<void(int, float)>([](int a, float b){
// do something
}));
Works flawlessly, even when omitting the Callback template arguments (like shown here). There is still the 'extra' std::function though.
Why cant it figure out the conversion on its own? It works for non-templates:
void B(std::function<void(int, float)> f){/* ... */};
int main(){
B([](int a, float b){
// do something
});
}
For reference, I'm using
gcc version 4.7.2 (Debian 4.7.2-5)