I want to be able to determine the number of bytes that are in a subset of a parameter pack from 0 to a given index.
Right now I'm using a non-constexpr way of doing this. Below is my code:
template <size_t index, typename... args> struct pack_size_index;
template <size_t index, typename type_t, typename... args>
struct pack_size_index <index, type_t, args...> {
static const size_t index_v = index;
static const size_t value(void) {
if (index_v > 0) {
return sizeof(type_t) + pack_size_index<index - 1, args...>::value();
}
return 0;
}
};
template <size_t index> struct pack_size_index <index> {
static const size_t index_v = index;
static const size_t value(void) { return 0; }
};
Usage:
//output: 5 (equal to 1 + 4)
std::cout << pack_size_index<2, bool, float, int, double>::value() << std::endl;
//output: 20 (equal to 8 + 8 + 4)
std::cout << pack_size_index<3, double, double, float, int>::value() << std::endl;
This gets the job done, but this uses runtime comparison and the resulting executable increases in size rapidly whenever this is used. What's a less expensive way of doing this?