1

I have a strange compile error with GCC 5.1.0 (tdm64-1) compiling the following code

template <class T>
struct test {
    template <class X>
    bool func();
};

template <class X, class Y>
bool testfunc(X x, Y y)
{
  test<Y>  s;

  if (s.func<X>())    // <-- parse error here
      return false;
}

void func2()
{
 testfunc( double(), long() );
}

The error is

testX.cpp: In function 'bool testfunc(X, Y)': 
testX.cpp:12:15: error: expected primary-expression before '>' token 
   if (s.func<X>()) 
               ^ 
testX.cpp:12:17: error: expected primary-expression before ')' token 
   if (s.func<X>()) 

Note that the error only occurs in the version when Y is a template parameter. When I remove the Y template parameter and instantiate test with a known type (e.g. test< int>), it compiles without error.

So what is wrong here?

Andreas H.
  • 4,439
  • 15
  • 22

2 Answers2

4

Change this:

if (s.func<X>()) 

To this:

if (s.template func<X>())

The reason is the s.func<X> is a dependant name so the compiler is not able to tell that func is a template member function.

Smeeheey
  • 9,162
  • 16
  • 37
3

Since s.func<X>() is a dependent name, you need to tell the compiler that func is a template, using the template keyword:

s.template func<X>()
Community
  • 1
  • 1
TartanLlama
  • 59,364
  • 11
  • 141
  • 183
  • awesome. But where is the ambiguity?Does the template keyword only specify that func is a template, or that it is a template member function? Even if I tell the compiler that it is a template, the compiler still does not know whether it is a member or a function. (Well perhaps this is clear from the syntax) – Andreas H. Sep 09 '16 at 08:00
  • 1
    @AndreasH. You can't have template member variables, so it's clear from the syntax. The trouble is with the `` tokens, which are heavily overloaded in C++. – TartanLlama Sep 09 '16 at 08:42