expected primary-expression before ')' token

Hello,

Here is some code which generates an error :

1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename T>
  struct A
{
  template <int i> static void f(void)
  {
  }
};

template <typename T>
  void f2(void)
{
  A<T>::f<0>();
}


The generated error is : "expected primary-expression before ')' token" on line 12.


Thank you to help me to understand where the problem is and suggest a correct syntax.
Last edited on
Very strange error. It compiles on VS 2015 CE, but not on cpp.sh
This works on the shell:
1
2
3
4
5
template <typename T>
void f2(void)
{
  A<T>::f();
}
Some more information about the problem :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
template <typename T>
  struct A
{
  template <int i> static void f(void)
  {
  }

  static void g(void)
  {
  }
};

struct B
{
  template <int i> static void f(void)
  {
  }
};

template <typename T>
  void f2(void)
{
  A<T>::f<0>(); // don't work

  A<T>::g(); // work
  A<int>::f<0>(); // work
  B::f<0>(); // work
}


Last edited on
I think you should use like this:
A<T>::template f<0>();

If you write like this A<T>::f<0>(); It is ambiguous for compiler, it can not deduce surely that f is a template function in template class A, it may take it as ((A<T>::f) < 0) >(), and would not compile correctly, the additional template is suggested to compiler that f is a template function.
It is the same case when we use typename like this:
class A {
typename B::IntType *i;
};
Hi,

I am going to guess a bit here :+|

On line 23, its a function call, but what is T here? The T refers to the parameter in the second template. In other words the scope of the T in the first template ends on line 18. The other problem is that A::f<0>() and A::f<1>() are different types, so that might be why it is rejected.

Line 25 Maybe that is to do with template argument deduction. g() is a static function with no arguments returning void, so that is ok, only because it didn't have it's own template parameter like f() does.

Line 26 works because you have specified the int (The T in the first template )

Line 27 works because it doesn't involve T , the 0 specifies the int, so everything is known.

Btw there is no need to specify void for a function with no parameters, this has never been a requirement for C++, instead it's something C programmers do.
Thank you very much for your answers.

The solution of Jun Zhang2 is working perfectly.

I was expecting some ambiguity of the "<" and ">" symbols but didn't know how to solve the issue.
Registered users can post here. Sign in or register to post.