Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the i-th integer in a integer parameter pack?

How to get the i-th integer in a integer parameter pack? For example

template<int... Is>
struct A
{
    enum { CONSTANT_0 = Is[0] }; //Assume the sizeof...(Is) > the index requested
};
like image 806
user1899020 Avatar asked Apr 29 '26 03:04

user1899020


1 Answers

Like that:

template <size_t I, int N, int... R>
struct pick : pick <I - 1, R...> { };

template <int N, int... R>
struct pick <0, N, R...> : std::integral_constant <int, N> { };

so that

pick <3, 1, 2, 3, 4, 5, 6>::value

equals 4, and

template<int... Is>
struct A
{
    enum { CONSTANT_0 = pick <0, Is...>::value };
};

is how you would use it in your case.


Another way:

template <size_t I, int... N>
using pick = typename std::tuple_element <I,
   std::tuple <std::integral_constant <int, N>...>
>::type;
like image 104
iavr Avatar answered May 01 '26 15:05

iavr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!