0

There have been a couple questions related to how to work around this issue:

I'm not interested in work arounds, I'd just like an explanation of why this doesn't work.

template <typename T>
void foo(T input, function<void(T)> func) { func(input); }

When I make a call like this: foo(13, [](int input){ cout << input; })
I'm assuming that the issue has to do with the fact that the lambda is not of type function<void(int)> but must be converted to that type. And I'm assuming that such a conversion is not permitted to successfully compile a template.
Could someone get me a reference that affirms this?

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 1
    Template deduction rules say the types must be an exact match. Is this what you're looking for? – Passer By Apr 09 '18 at 13:19
  • For the template to deduce that, it would have to convert the lambda to a `std::function`. That's not how template deduction works. – super Apr 09 '18 at 13:21
  • this will work as you help to deduct the type. template void foo(T input, std::function func) { func(input); } foo(13, [](int input) { std::cout << input; }); – Arkady Godlin Apr 09 '18 at 13:26

0 Answers0