2 Comments

Could you explain why this is better than virtual methods? In the end you’re still referencing the implementation through a pointer, hence introducing the same level of indirection

Expand full comment
author

It isn't better than virtual methods in the sense, that you are effectively re-implementing the same functionality, so at best it will be equivalent performance (although this demo doesn't use double-indirection through an array of function pointers).

However, the benefit is that this approach isn't intrusive, i.e. the types that you are erasing do not have any overhead (no pointer to vtable, they can remain trivially copyable, they can use aggregate initialization, they can have standard layout, and so on...).

Expand full comment