I have a variadic Engine template class:
template <typename ... Components> class Engine;
I'd like to assign a number to each component at compile time which is equivalent to their ordering. This would be returned when making the following call:
template <typename Component> int ordinal();
So for example if:
Engine<PositionComponent, PhysicsComponent, InputComponent> engine;
was declared, the call:
engine.ordinal<PhysicsComponent>();
would return 1 and a similar call with InputComponent instead of PhysicsComponent would return 2.
Is it possible, and if yes, how would one go about it?
In Boost.Mp11, this is a short one-liner (as always):
Note that if
Component
is absent, this will returnsizeof...(Components)
. If desired, you can add a static assertion to verify this.My original answer follows below the fold...
So you want to find the index of
Component
inComponents...
?Usage:
As constructed, trying to get the
ordinal
of aComponent
not inComponents...
will be a compile error. Which seems appropriate.