Suppose I have an interface I, and a templated class C which implements the interface:
class I {
public:
virtual int foo() = 0;
};
template<typename T>
class C : public I {
public:
int foo() override;
};
I want there to be only two instantiations of C, with types T1 and T2. I want to specialize foo() for each of them. I can do this in my .cpp file like this:
template<>
C<T1>::foo() { return 1; }
template<>
C<T2>::foo() { return 2; }
template class C<T1>;
template class C<T2>;
I am wary that someone will come along with more template parameters they want to use for C, such as T3 and T4, and will define a generic foo()
template<typename T>
C::foo() { return 0; }
// (with appropriate specialization declarations)
template class C<T3>;
template class C<T4>;
because then, in a final scenario, someone could add a fifth type and forget to specialize foo() for it:
// forgotten:
// template<>
// C::<T5>::foo() { return 5; }
template class C<T5>;
How can I ask the compiler to complain about someone adding the generic foo(), either in a unit test or in the implementation of C?