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?
Here’s one technique that I know of.
First, make another template class
policy_enforcer. Declare this class without defining it, and also provide a specialization of it forderivedthat is also defined:Then, within the function you wish to lock down, include the expression
sizeof(policy_enforcer<T>). Sincesizeofon incomplete types is a compilation error, this will prevent the code from compiling.Updated with live code: using
base, usingderived, usinglowest.