This question already has an answer here:
- Where and why do I have to put the “template” and “typename” keywords? 6 answers
(This question is only a duplicate of the other question if you already know the answer!)
(Please note my follow-up question: Why is no template keyword needed if an unrelated global template function with same name exists?)
I get a compiler error at the indicated line when I try to compile templated C++ code with this structure:
template <int N>
struct A {
template <int i>
void f() {};
};
template <int N>
struct B {
A<N> a;
B(A<N>& a) : a(a) {}
void test() {
a.f<1>(); // does not compile
}
};
int main() {
A<2> a;
a.f<1>(); // works fine
B<2> b(a);
b.test();
}
g++
says:
test2.cpp: In member function ‘void B<N>::test()’:
test2.cpp:14: error: expected primary-expression before ‘)’ token
test2.cpp: In member function ‘void B<N>::test() [with int N = 2]’:
test2.cpp:22: instantiated from here
test2.cpp:14: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator<’
clang++
says:
test2.cpp:14:16: error: expected expression
a.f<1>(); // does not compile
^
1 error generated.
The same expression works in the context of main()
, but not within the test()
method of the templated class B
, which has an instance of A
as a private variable. I am pretty clueless why this doesn't work.