0
template <typename T>
class X
{
public:
  template <typename Y>
  void x();
};

template<typename T>
class Z
{
public:
  void x()
  {
    void (X<Z>::*p)() = &X<Z>::x<T>;    
  }
};

g++ 4.8.1

How do I get this to compile please; I have tried many variants but get the following errors

t.C:15:35: error: expected primary-expression before ‘>’ token
                                   ^
t.C:15:36: error: expected primary-expression before ‘;’ token
                                    ^
user3286380
  • 574
  • 4
  • 10
njw
  • 1

2 Answers2

4

When a dependent name is a template, you need the template keyword:

void (X<Z>::*p)() = &X<Z>::template x<T>;

If you don't the compiler thinks x is a normal variable that is a member of X, because it doesn't check. You must explicitly inform the compiler that the name is a template. It is the counterpart to the typename keyword used to disambiguate type names from variables names.

user3286380
  • 574
  • 4
  • 10
1

X<Z>::x is a dependent name (i.e. its meaning depends on the template parameter Z), so you have to specify that x is a template:

void (X<Z>::*p)() = &X<Z>::template x<T>;
                           ^^^^^^^^
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644