C ++调用模板函数作为模板参数

时间:2014-05-07 16:28:14

标签: c++ templates compiler-errors

我有错误“预期'int'之前的primary-expression”和“expected';'在'int'之前,在这段代码中,有人知道为什么吗?

struct cpu
{
    template<class T> static void fce() {}
};

template<class _cpu>
struct S
{    
    void fce() 
    {  
        _cpu::fce<int>(); //error: expected primary-expression before 'int'
                          //error: expected ';' before 'int'
    }
};

int main()
{
    S<cpu> s;
    s.fce();
}

1 个答案:

答案 0 :(得分:3)

_cpu::template fce<int>(); 

尝试以上方法。

由于_cpu是template参数,编译器无法识别fce可能是member function的名称 - 因此它将<解释为小于template符号。 为了使编译器能够识别函数模板调用,请添加{{1}}量词:

相关问题