I know you can overload templates based on their template parameters:
template <class T> void test() {
std::cout << "template<T>" << std::endl;
}
void test() {
std::cout << "not a template" << std::endl;
}
then inside some function:
test<int>();
test();
will correctly resolve which of the 2 different versions of test() you want. However, if I now do this inside classes with inheritance:
class A {
public:
void test() {
std::cout << "A::Test: not a template" << std::endl;
}
};
class B : public A {
public:
template <class T>
void test() {
std::cout << "B::Test: template<T>" << std::endl;
}
};
then inside a function:
B b;
b.test<int>();
b.test();
b.test<int>(); works but b.test(); does not:
error: no matching function for call to ‘B::test()’
note: candidate is:
note: template<class T> void B::test()
Why is this/ is there any way to make it correctly resolve the 2 versions based on the template arguments?
As always, a name defined in a derived class hides uses of the same name in a base class. To hoist the name in the base class into the derived class, add
to the derived class.