Question title: How to extract all tuple elements of given type(s) into new tuple
Question body: 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 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 and std::is_same_v per element could work.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Yes, you're on the right track with std::make_index_sequence and std::is_same_v. Here's a possible implementation for extract_from_tuple that achieves the desired behavior:
cpp
#include <tuple>
#include <utility> // for std::make_index_sequence
#include <type_traits> // for std::is_same_v, std::tuple_cat
// Helper function to extract elements of specified types from a tuple
template<typename... Ts_out, std::size_t... Is, typename Tuple>
constexpr auto extract_from_tuple_impl(std::index_sequence<Is...>, Tuple& tuple) {
// Use a fold expression to iterate over the indices and extract matching types
return std::tuple_cat(
...(std::is_same_v<std::tuple_element_t<Is, Tuple>, Ts_out> // Check if the type at index Is matches any of Ts_out
? std::make_tuple(std::get<Is>(tuple)) // If it matches, create a tuple with the element
: std::tuple<>)... // If not, create an empty tuple
);
}
// Main function to extract elements of specified types from a tuple
template<typename... Ts_out, typename Tuple>
constexpr auto extract_from_tuple(Tuple& tuple) {
// Generate an index sequence for the tuple and call the implementation
return extract_from_tuple_impl<Ts_out...>(
std::make_index_sequence<std::tuple_size_v<Tuple>>{}, // Create an index sequence for the tuple
tuple
);
}
Let's break down the implementation:
extract_from_tuple_impl:
This is a helper function that takes an std::index_sequence (generated by std::make_index_sequence) and a reference to the input Tuple.
The function uses a fold expression to iterate over the indices Is in the std::index_sequence, checking each element's type against the desired types Ts_out.
Matching elements are wrapped into singleton tuples, while non-matching elements result in empty tuples.
std::tuple_cat concatenates these tuples into a single tuple containing all matching elements.
extract_from_tuple:
This main function generates an std::index_sequence covering all indices of the input tuple and forwards it to extract_from_tuple_impl.
Edge Cases
Empty Tuple: If the input tuple is empty, std::make_index_sequence will produce an empty sequence, and extract_from_tuple_impl will return an empty tuple, as there are no elements to process.
No Matching Types: If no types in the tuple match Ts_out, the function will return an empty tuple, as all iterations will produce empty tuples that std::tuple_cat will concatenate into a single empty tuple.
Now, let's test the implementation with your example: