根据这个问题的第一个答案:函数模板重载,"非模板化(或"模板化程度较低")重载优于模板".
#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!"); …Run Code Online (Sandbox Code Playgroud)