在下面的代码示例中,对foo
的调用有效,而对bar
的调用失败。将函数作为显式模板参数传递
如果我将bar
的调用注释掉,代码将会编译,它告诉我bar
本身的定义是正确的。那么bar
如何被正确调用?
#include <iostream>
using namespace std;
int multiply(int x, int y)
{
return x * y;
}
template <class F>
void foo(int x, int y, F f)
{
cout << f(x, y) << endl;
}
template <class F>
void bar(int x, int y)
{
cout << F(x, y) << endl;
}
int main()
{
foo(3, 4, multiply); // works
bar<multiply>(3, 4); // fails
return 0;
}
另请参阅[函数作为模板参数传递](https://stackoverflow.com/q/1174169/608639)。 – jww