Re:Final features of C++17
by im3w1l on Friday July 01, 2016 @01:49PM
(#12018187)
Nice! Now to make the idea of compile time virtual functions real, can you force derived classes to have foo_impl, and have an inlined function foo in the base class that calls foo_impl?
What about grand children classes?
[ Parent
| on HN ]
Re:Final features of C++17
by Kristine1975 on Friday July 01, 2016 @02:17PM
(#12018435)
>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.
Doesn't you code assume that there is exactly one derived class, and that the base class knows the name of it?
Re:Final features of C++17
by Kristine1975 on Saturday July 02, 2016 @07:32AM
(#12022153)
The 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_p...