Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading