【发布时间】:2021-08-13 01:12:10
【问题描述】:
有什么方法可以调用类模板Foo的函子operator()( int ),如下图(online version)
template<typename T>
struct Foo
{
template<typename U>
void operator()( int )
{
}
};
int main(int argc, char *argv[])
{
Foo<char> foo;
foo<bool>( 42 );
}
我在 gcc 4.9.3 中收到错误消息
error: expected primary-expression before ‘bool’
foo<bool>( 42 );
如果成员函数不是函子并且以::、. 或-> 为前缀,我会在函子前面加上template。没有一些帮助,编译器不知道如何解析这个表达式;作为 foo<int> 类型的匿名对象的仿函数或实例化。
【问题讨论】: