1

I have error "expected primary-expression before 'int'" and "expected ';' before 'int'" in this code, does anyone know why?

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();
}
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271

1 Answers1

3
_cpu::template fce<int>(); 

Try the above.

Since _cpu is a template parameter, the compiler does not recognize that fce may be the name of a member function - hence it interprets the < as a less than symbol. In order for the compiler to recognize the function template call, add the template quantifier:

user93353
  • 13,733
  • 8
  • 60
  • 122