Calling a member function of a template that does not compile

I have a problem with a code snippet like this:

template<class Type>
class Class1 {
public:
    template<class TypeName1> TypeName1* method1() const {return 0;}
};

struct Type1{};
struct Type2{};

class Class2 {
public:
   template<typename TypeName1, typename TypeName2>
   int method2() {
       Class1<TypeName2> c;
       c.method1<TypeName1>();
       return 0;
   }

   int method1() {
       return method2<Type1, Type2>();
   }
};

int
main() {
   Class2 c;
   return c.method1();
}

When compiling with a compiler in codedex:

http://codepad.org/ZR1Std4k

I get the following error:

t.cpp: In the member function 'int Class2 :: method2 ()': Line 15: error: expected primary expression before completion of compilation '>' due to -Wfatal-errors.

The violation string is a call to the member function of the template:

c.method1<TypeName1>();
+5
source share
1 answer

You must use the keyword templatewhen calling the member function template, and you have a dependent name, or it method1will be parsed as a member variable cand <as "less than":

c.template method1<TypeName1>();

@DrewDormann, , template, , Class1 , method1 - . , method1 , .

+11

All Articles