How I should make sure that classes needed as a one of params to my templated class will have a certain interface? I know that one way is to make in Interface class pure virtual but I would like to avoid it. Is there any other way to do it? I would like to avoid anything which isn’t standard
//Example
template<class SomePolicy>
class My
{
void fnc()const
{
SomePolicy::mustHaveThisInterface();//<--here I have to have
// this interface in orded to work
}
};
A simple solution is to have a dummy function that validates the requirement. Example:
Note that, since this is a template,
My<T>::fnc()won’t be compiled unless it is called, which means your validationrequire<I,T>()directive must be placed somewhere important (e.g. the constructor, or at the beginning of each function whereSomePolicymust inheritInterface.) Just how many checks you put depends on your level of paranoia.Performance Note: since the check amounts to a no-op, any decent compiler will optimize it out so it won’t cost anything in run time or memory. The compiler is still obliged to make it compiile though, which means it can’t skip the test at compile-time.