I am trying to work out some way to enforce contract between base CRTP class and the derived one. When using dynamic polymprhpism, one can simply do:
struct foo{
virtual bar() = 0;
};
And the compiler will make sure that the method bar
is implemented in deriving class, otherwise it will give (more or less) meaningful error message. Now the best I could get with CRTP to is something like this:
template<class Derived>
struct base {
void stuff(){
static_cast<Derived &>(*this).baz();
}
};
Which will somehow enforce implementation of baz
in derived class but is not really readable and clear what the contract between base and derived class is. So my question is, is there a way to do this better? I am aware of C++20 Concepts and that they would be perfect solution for this case, but I am looking for C++11/C++14 solution to make it as clean as possible.