I have a template function and wish to ensure at compile time that it is not instantiated on a subtype or supertype of a particular class.
How can I cause a C++ compiler error if this is violated?
class base {
};
class derived : public base {
};
class lowest : public derived {
};
template <typename T>
bool isCorrect(const T& obj) {
typedef foo<T> D;
foo<T> *def = foo<T>::find();
return (def && def->getAnswer(object));
}
I want isCorrect
to only be available for class derived
, but not base
or lowest
. Note there could be many other lowest classes and a string of base classes to be excluded as well as alternative derived classes that are acceptable.
Is there a way in C++ to limit the template to only apply to the derived classes I explicitly specify?