Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract all tuple elements of given type(s) into new tuple

The existing tuple overloads of std::get are limited to return exactly 1 element by index, or type. Imagine having a tuple with multiple elements of the same type and you want to extract all of them into a new tuple.

How to achieve a version of std::get<T> that returns a std::tuple of all occurrences of given type(s) like this?

template<typename... Ts_out>
constexpr std::tuple<Ts_out...> extract_from_tuple(auto& tuple) {
    // fails in case of multiple occurences of a type in tuple
    return std::tuple<Ts_out...> {std::get<Ts_out>(tuple)...};
}

auto tuple = std::make_tuple(1, 2, 3, 'a', 'b', 'c', 1.2, 2.3, 4.5f);
auto extract = extract_from_tuple <float, double>(tuple);
// expecting extract == std::tuple<float, double, double>{4.5f, 1.2, 2.3}

Not sure if std::make_index_sequence for accessing each element by std::get<index> and std::is_same_v per element could work.

like image 691
Chris G. Avatar asked Nov 27 '25 09:11

Chris G.


1 Answers

Only C++17 is needed here.

std::tuple_cat is one of my favorite tools.

  1. Use a std::index_sequence to chew through the tuple

  2. Use a specialization to pick up either a std::tuple<> or a std::tuple<T> out of the original tuple, for each indexed element.

  3. Use std::tuple_cat to glue everything together.

  4. The only tricky part is checking if each tuple element is wanted. To do that, put all the wanted types into its own std::tuple, and use a helper class for that part, too.

#include <utility>
#include <tuple>
#include <iostream>

// Answer one simple question: here's a type, and a tuple. Tell me
// if the type is one of the tuples types. If so, I want it.

template<typename wanted_type, typename T> struct is_wanted_type;

template<typename wanted_type, typename ...Types>
struct is_wanted_type<wanted_type, std::tuple<Types...>> {

    static constexpr bool wanted=(std::is_same_v<wanted_type, Types>
                      || ...);
};

// Ok, the ith index in the tuple, here's its std::tuple_element type.
// And wanted_element_t is a tuple of all types we want to extract.
//
// Based on which way the wind blows we'll produce either a std::tuple<>
// or a std::tuple<tuple_element_t>.

template<size_t i, typename tuple_element_t,
     typename wanted_element_t,
     bool wanted=is_wanted_type<tuple_element_t, wanted_element_t>::wanted>
struct extract_type {

    template<typename tuple_type>
    static auto do_extract_type(const tuple_type &t)
    {
        return std::tuple<>{};
    }
};


template<size_t i, typename tuple_element_t, typename wanted_element_t>
struct extract_type<i, tuple_element_t, wanted_element_t, true> {

    template<typename tuple_type>
    static auto do_extract_type(const tuple_type &t)
    {
        return std::tuple<tuple_element_t>{std::get<i>(t)};
    }
};

// And now, a simple fold expression to pull out all wanted types
// and tuple-cat them together.

template<typename wanted_element_t, typename tuple_type, size_t ...i>
auto get_type_t(const tuple_type &t, std::index_sequence<i...>)
{
    return std::tuple_cat( extract_type<i,
                   typename std::tuple_element<i, tuple_type>::type,
                   wanted_element_t>::do_extract_type(t)... );
}


template<typename ...wanted_element_t, typename ...types>
auto get_type(const std::tuple<types...> &t)
{
    return get_type_t<std::tuple<wanted_element_t...>>(
        t, std::make_index_sequence<sizeof...(types)>());
}

int main()
{
    std::tuple<int, const char *, double> t{1, "alpha", 2.5};

    std::tuple<double, int> u=get_type<int, double>(t);

    std::cout << std::get<0>(u) << " " << std::get<1>(u) << std::endl;

    std::tuple<int, int, int, char, char, char, double, double, float> tt;

    auto uu=get_type<float, double>(tt);

    static_assert(std::is_same_v<decltype(uu),
              std::tuple<double, double, float>>);

    return 0;
}
like image 112
Sam Varshavchik Avatar answered Nov 29 '25 00:11

Sam Varshavchik



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!