4 ms·
>can you force derived classes to have foo_impl, and have an inlined function foo in the base class that calls foo_impl? If that's all you want just write:
by Kristine1975 10y ago
>can you force derived classes to have foo_impl, and have an inlined function foo in the base class that calls foo_impl?
If that's all you want just write:
void foo()
{
static_cast<Derived&>(*this).foo_impl();
}
When the derived class doesn't have any method "foo_impl" (or no "foo_impl" with a suitable signature), you will get a compile-time error.
No need for the static_assert, that was just because KKKKkkkk1 asked whether checking for the existence of a method at compile-time was possible.
Compile-time polymorphism is essentially duck typing: If it quacks like a duck you can treat it as one. If it doesn't quack like a duck, but you still treat it as one you get a compiler error.
- im3w1l 10y agoDoesn't you code assume that there is exactly one derived class, and that the base class knows the name of it?
- Kristine1975 10y agoThe base class must be a template with one parameter being the derived class: template <typename D> class Base { ... }; and then: class Derived: public Base<Derived> { ... }; This is called the https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern https://en.wikipedia.org/wiki/Curiously_recurring_template_p...