C++ overloaded function as template argument
问题 the simplified version of my code is here int foo(int x) { return x; } int foo(int x, int y) { return x+y; } template<typename unary_func> int bar(int k, unary_func f) { return f(k); } int main() { bar(3, foo); return 0; } Is there a way to tell the compiler what I want to pass as argument is the first `foo'? 回答1: You can give an explicit template argument: bar<int(int)>(3, foo); or cast the ambiguous function name to a type from which the template argument can be deduced: bar(3, static_cast