sample code given below is not compiled in g++. but it’s working on visual studio.
is it possible to use Template member function inside template class in g++
class Impl
{
public:
template<class I>
void Foo(I* i)
{
}
};
template<class C>
class D
{
public:
C c;
void Bar()
{
int t = 0;
c.Foo<int>(&t);
}
};
int main()
{
D<Impl> d;
d.Bar();
return 0;
}
Because the statement in question depends on a template parameter, the compiler is not allowed to introspect
Cuntil instantiation. You must tell it that you mean a function template:If you don’t put
templatethere, the statement is ambiguous. For understanding, imagine the followingC:It looks to the compiler as if you compare a
const intto anint, and comparing the result of that to some adress of&t:(c.Foo<int) > &t.The real solution however is to omit the explicit template argument in the function call, and just do:
This is correct even in the case where such a
Chas a non-template member functionFoo(int). Generally, write template code with as few assumptions as possible (but not less).