How to call a template member function? [duplicate]
Asked Answered
P

2

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?

Petronilapetronilla answered 1/10, 2012 at 15:21 Comment(1)
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.Petronilapetronilla
P
69

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>();
Petronilapetronilla answered 1/10, 2012 at 15:25 Comment(1)
And this is done for the same reason and in conditions similar to when typename is necessary.Multitudinous
M
14

you can call the function this way:

b.template foo<T0>();
Maxson answered 1/10, 2012 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.