40

Possible Duplicate:
C++ template member function of template class called from template function

template<class T1>
class A 
{
public:
    template<class T0>
    void foo() const {}
};

template<class T0,class T1>
void bar( const A<T1>& b )
{
    b.foo<T0>();  // This throws " expected primary-expression before ‘>’ token"
}

I can change it to

b->A<T1>::template foo<T0>();

which compiles fine. However I can also change it to

b.A<T1>::template foo<T0>();

which compiles fine too. eh?

How does one correctly call the template member function in the sense of the original code?

Dean Seo
  • 5,486
  • 3
  • 30
  • 49
ritter
  • 7,447
  • 7
  • 51
  • 84
  • Do we know what the two statements that actually compile mean? In this "cooked down" example they compile, but in my real program this does not compile. – ritter Oct 01 '12 at 15:37

2 Answers2

68

Just found it:

According to C++'03 Standard 14.2/4:

When the name of a member template specialization appears after . or -> in a postfix-expression, or after nested-name-specifier in a qualified-id, and the postfix-expression or qualified-id explicitly depends on a template-parameter (14.6.2), the member template name must be prefixed by the keyword template. Otherwise the name is assumed to name a non-template.

Correct code is:

b.template foo<T0>();
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
ritter
  • 7,447
  • 7
  • 51
  • 84
  • 2
    And this is done for the same reason and in conditions similar to when `typename` is necessary. – bames53 Oct 01 '12 at 16:10
14

you can call the function this way:

b.template foo<T0>();
Ninten
  • 565
  • 3
  • 10