Re: Virtual method inlining
On 9 nov, 10:39, Leigh Johnston <le...@i42.co.uk> wrote:
On 09/11/2012 07:12, 1 2 wrote:
On 8 nov, 18:28, Leigh Johnston <le...@i42.co.uk> wrote:
On 08/11/2012 20:15, 1 2 wrote:
And in the code that uses the polymorphic operations, we use a
template parameter, but make sure that it implements the interface by
using a static_assert that tests a std::is_base_of:
template<class Shape>
void DrawShape(Shape* s)
{
static_assert(std::is_base_of<IShape, Shape>::value, "Sha=
pe must
implement IShape");
s->Draw(200, 100);
}
I can definitely see this being used at large scale.
What is the point of doing that? The compiler doesn't know that Dra=
w is
"final" so still requires dynamic (virtual dispatch) unless it knows
Shape is the most derived type; the "final" overrider in C++11 improve=
s
the situation somewhat.
OK, then maybe it can be changed to the following to disable calling
through the vtable:
template<class Shape>
void DrawShape(Shape* s)
{
static_assert(std::is_base_of<IShape, Shape>::value, "Shape =
must
implement IShape");
s->Shape::Draw(200, 100);
}
Totally pointless.
Why? It accomplishes what I said earlier: combine the contracts
provided by runtime polymorphism with the fast (w/o virtual dispatch)
calls of compile-time polymorphism.