Getting inspiration from this question, I managed to implement a function which takes two tuples as input and return a tuple which components are the min of each components of the tuples given in input.
template<typename T, T...> struct integer_sequence { }; template<std::size_t N, std::size_t... I> struct gen_indices : gen_indices<(N - 1), (N - 1), I...> { }; template<std::size_t... I> struct gen_indices<0, I...> : integer_sequence<std::size_t, I...> { }; template <typename ... T, std::size_t ... I> std::tuple<T...> min_tuple_impl( const std::tuple<T...>& t1, const std::tuple<T...>& t2, integer_sequence<std::size_t, I...>) { return { (std::min(std::get<I>(t1), std::get<I>(t2)))... }; } template <typename ... T> std::tuple<T...> min_tuple( const std::tuple<T...>& t1, const std::tuple<T...>& t2) { return min_tuple_impl(t1, t2, gen_indices<sizeof...(T)>{}); }
Now, I would like it to work on all components but the last one.
std::tuple<int, int, int> t1 {10, 20, 30} std::tuple<int, int, int> t2 {15, 15, 15} min_tuple(t1, t2) // == {10, 15, 30}, for the last one, always keep the component of t1
How could I get this in C++11?
https://stackoverflow.com/questions/67426161/arithmetic-on-all-but-the-last-components-of-tuples-in-c May 07, 2021 at 05:26AM
没有评论:
发表评论