According to the first answer to this question: function template overloading, a "A non-templated (or "less templated") overload is preferred to templates".
#include <iostream>
#include <string>
#include <functional>
void f1(std::string const& str) {
std::cout << "f1 " << str << "\n";
}
template <typename Callback, typename... InputArgs>
void call(Callback callback, InputArgs ...args) {
callback(args...);
}
void call(std::function<void(std::string const&)> callback, const char *str) {
std::cout << "custom call: ";
callback(str);
}
int main() {
auto f2 = [](std::string const& str) -> void {
std::cout << "f2 " << str << "\n";
};
call(f1, "Hello World!");
call(f2, "Salut Monde !");
return 0;
}
Where, as far as I understand it, the second definition of call
is "non-templated", and thus should be chosen over the first one when I do call(f1, "1")
or call(f2, "2")
.
This is not the case, I get the following output:
f1 Hello World!
f2 Salut Monde !
If I remove the templated version of call
, I get the expected output.
Why is my overload of call
not chosen over the first one in this case?