展开
tuple
使用模板lambda,并选择是否基于当前元素的索引应用该函数
#include <tuple>
template<size_t index, size_t I, typename Func, typename Tuple>
auto transform_helper(Tuple& tup, Func& fn) {
if constexpr (I < index)
return std::get<I>(tup);
else
return fn(std::get<I>(tup));
}
template<size_t index, typename Func, typename... Args>
auto transform(std::tuple<Args...> tup, Func fn) {
return [&]<std::size_t... I>(std::index_sequence<I...>) {
return std::tuple{transform_helper<index, I>(tup, fn)... };
}(std::index_sequence_for<Args...>{});
}
Demo