I have a variadic Engine template class:
template class Engine;
I\'d like to assign a number to each compon
My goal below is to keep things in the compile-time realm as much as possible.
This is an alias to remove some boilerplate. std::integral_constant
is a wonderful std
type that stores a compile-time determined integer-type:
template
using size=std::integral_constant;
Next, a index_of
type, and an index_of_t
that is slightly easier to use:
templatestruct types{using type=types;};
templatestruct index_of{};
template
struct index_of>:size<0>{};
template
struct index_of>:size<
index_of>::value +1
>{};
This alias returns a pure std::integral_constant
, instead of a type inheriting from it:
template
using index_of_t = size< index_of>::value >;
Finally our function:
template
static constexpr index_of_t
ordinal() const {return{};}
it is not only constexpr
, it returns a value that encodes its value in its type. size>
has a constexpr operator size_t()
as well as an operator()
, so you can use it in most spots that expect integer types seamlessly.
You could also use:
template
using ordinal = index_of_t;
and now ordinal
is a type representing the index of the component, instead of a function.