According to the first answer to this question: overloading the function template , "templates are not preferred" (or "less template").
#include <iostream>
#include <string>
#include <functional>
void f1 (std::string const& str) {
std::cout << "f1 " << str << std::endl;
}
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 << std::endl;
};
call(f1, "Hello World!");
call(f2, "Salut Monde !");
return 0;
}
Where, as I understand it, the second definition callis "not templated" and therefore should be chosen by the first when I do call(f1, "1")or call(f2, "2").
This is not so, I get the following output:
f1 Hello World!
f2 Salut Monde !
If I remove the template version call, I will get the expected result.
Why is my overload callnot selected in the first case in this case?
source
share