Is there any way to convert a parameter pack of types to a parameter pack of integers from 0
to sizeof...(Types)
? More specifically, I'm trying to do something this this:
template <size_t... I>
void bar();
template <typename... Types>
void foo() {
bar<WHAT_GOES_HERE<Types>...>();
}
For example, foo<int,float,double>()
should call bar<0, 1, 2>()
;
In my use case the parameter pack Types
may contain the same type multiple times, so I cannot search the pack to compute the index for a given type.
In C++14 you can use std::index_sequence_for
from the <utility>
header along with tagged dispatch. This is known as the indices trick:
template <std::size_t... I>
void bar(std::index_sequence<I...>);
template <typename... Types>
void foo() {
bar(std::index_sequence_for<Types...>{});
}
If you are limited to C++11, you can find many implementations of the above online, such as this one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With