【发布时间】:2012-06-07 22:10:18
【问题描述】:
在下面的代码示例中,对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;
}
【问题讨论】: