Andreas H.
Andreas H.

Reputation: 6095

Strange compile error when calling explicitly specialized class member template function

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?

Upvotes: 1

Views: 56

Answers (2)

TartanLlama
TartanLlama

Reputation: 65620

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>()

Upvotes: 3

Smeeheey
Smeeheey

Reputation: 10326

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.

Upvotes: 4

Related Questions