I have to following example code. In this code I have two template classes each with a template function. One of the classes has an object of the other class and in the template function it calls the template of the other class.
template<typename T>
class A{
public:
A(T a): a(a){}
template< typename V> V foo(){
return this->a;
}
private:
T a;
};
template<typename T>
class B{
public:
B( A<T> a): a(a){}
template<typename V>V foo2(){
return this->a.foo<V>();
}
private:
A<T> a;
};
int
main()
{
A<int> a(5);
double aTest = a.foo<double>();
B<int> b(a);
double c = b.foo2< double >();
}
I need to provide the template element after the function call, because automatic type deduction does not work for functions, were only the return type depends on the template parameter. This works for aTest. But when I add the next two lines I get the following compiler error:
build/main.cpp: In member function 'V B<T>::foo2()':
build/main.cpp:32:23: error: expected primary-expression before '>' token
return this->a.foo<V>();
^
build/main.cpp:32:25: error: expected primary-expression before ')' token
return this->a.foo<V>();