0

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)>)'
  1. 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.
  2. 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?
SwiftMango
  • 15,092
  • 13
  • 71
  • 136
  • 2
    A lambda isn't a `std::function`, and it won't deduce as one. – Passer By Dec 30 '18 at 12:48
  • 1
    However, there is a very simple workaround (deduction suppression for the 2nd param), and this can be an answer here - so it's not full duplicate. – Igor R. Dec 30 '18 at 13:56

0 Answers0