I have the following template declaration
template<typename T>
void foo(function<void(T)> f){
// ...
};
But when i call it like this
foo([](string s){ });
// visual studio 13 error message =>
// Error: void foo(std::function<void(_Type)>)' :
//could not deduce template argument for 'std::function<void(_Type)>'
//from 'main::<lambda_58b8897709e10f89bb5d042645824f66>
Template argument deduction fails . Why? how to fix it?
I have the same problem with variadic templates
template<typename ... Tn>
void foo(function<void(Tn ...)> f){
// ...
};
int main() {
foo<string,bool>([](string s,bool b){ }); // Works
foo([](string s,bool b){ }); // Fails
}
But if i explicitly cast the lambda it works (!)
foo((function<void(string,bool)>) [](string s,bool b){ }); // Works
// Or even a simpler syntax with a macro
#define lmda_(a) (function<void a>)[&] a
foo( lmda_((string s, bool b)) { }); // Works (note the extra () )
Why template argument deduction fails ? and how to fix it?