I have a base class template and a class deriving from an instantiation of it:
template<typename T>
class Bar
{
template <T t>
void Foo();
};
class Derived : public Bar<int> {};
How should one implement Derived::Foo<0>() for example? when trying this out:
template<>
void Derived::Foo<0>() { /* impl.. */}
i get the following compile error:
template-id 'Foo<0>' for 'Derived::Foo()' does not match any template declaration.
CodePudding user response:
You cannot specialize it in the descendant; you can only add a signature that'll serve as an overload and delegate as necessary:
template<typename T>
class Bar
{
template <T t>
void Foo();
};
class Derived : public Bar<int> {
using Base = Bar<int>;
template <int t>
void Foo()
{
if constexpr(t == 0) {
/* impl... */
} else {
Base::Foo<t>();
}
}
};
Since the ADL / Koenig lookup of a class does not include template base class(es), I'd not expect any issues during overload resolution if you do it this way.