c++arraysstltuplesmetaprogramming

getting index of current tuple item in std::apply


there is a call of std::apply for std::tuple,

inside the lambda, there is another container: std::array, each element of that is an object and I need to call this member with a parameter - tuple value.

something like this:

std::apply(
    [this](auto&&... tuple_item) {
         ((std::get<TUPLE_ITEM_INDEX???>(this->my_std_array_member.func(tuple_item.name), ...)
    },
    my_std_tuple
);

of course, supposed that tuple and my_array_member are equal (size). objects in a tuple are classes and all of those have a "name" field, which I need to use in array calls

how to get current tuple index in the expression to use it in get<>() for std::array ??


Solution

  • In this case, I don't think you should use std::apply but create a std::index_sequence that you supply to an immediately invoked lambda and use both as an index in the array and in std::get to get the tuple element:

    [&]<std::size_t... Is>(std::index_sequence<Is...>) {
        ((my_std_array_member[Is].func(std::get<Is>(my_std_tuple).name)), ...);
    }(std::make_index_sequence<std::tuple_size_v<decltype(my_std_tuple)>>{});
    

    Demo