I have the following code:
template<typename T>
void foo(T, std::vector<T>){}
template<typename T>
void bar(T, std::function<void(T)>){}
int main()
{
foo(0, std::vector<int>{});
bar<int>(0, [](int){});
}
foo
can work without explicitly specifying the template type, and bar
does not work if I do not specify the type like:
bar(0, [](int){}); //Compile error: no matching function for call to 'bar(main()::<lambda(int)>)'
- Why does the template deduction works for foo but not bar? How can I make it work? I do not want to template the whole type of function because I wanna make sure the function takes the parameter type T, the same as the first parameter.
- Since template type T can be deducted from the first parameter, can I do something like:
foo(0, std::vector<auto>{});
(I understand it does not compile). Any workaround?