Equivalent of std::transform for tuples

六眼飞鱼酱① 提交于 2019-11-30 18:54:16
TartanLlama

Here's a solution that uses index_range from here.

template<size_t SN, size_t DN, class TSrc, class TDest, class Func>
void tuple_call_assign(TSrc&& source, TDest& target, Func f)
{
    std::get<DN>(target) = f(std::get<SN>(std::forward<TSrc>(source))); 
}

template<size_t From, size_t To, class TSrc, class TDest, class Func, size_t...Is, size_t...DIs>
void tuple_transform(TSrc&& source, TDest& target, Func f,
                     std::index_sequence<Is...>, std::index_sequence<DIs...>)
{
    using expander = int[];
    (void)expander { 0, (tuple_call_assign<Is,DIs>(std::forward<TSrc>(source),target,f), 0)... };
}

template<size_t From, size_t To, size_t FromDest, class TSrc, class TDest, class Func>
void tuple_transform(TSrc&& source, TDest& target, Func f)
{
    static_assert(To > From, "Range must be increasing");
    static_assert(To <= std::tuple_size<std::decay_t<TSrc>>::value+1, 
        "Range must be valid for source tuple");
    constexpr size_t RangeSize = To-From;
    static_assert(FromDest+RangeSize <= std::tuple_size<std::decay_t<TDest>>::value, 
        "Range must be valid for target tuple");

    tuple_transform<From,To>(std::forward<TSrc>(source), target, f,
                    index_range<From,To>(), index_range<FromDest, FromDest+RangeSize>());
}

Demo

This takes a third template argument to specify a starting index to transform into the target tuple.

The index_sequence solution has been mentioned already:

template <std::size_t From, size_t... indices, typename T1, typename T2, typename Func>
void transform(T1&& s, T2& t, Func f, std::index_sequence<indices...>)
{
    (void)std::initializer_list<int>{
        (std::get<indices+From>(t) = f(std::get<indices>(std::forward<T1>(s))), 0)...};  
}


template <std::size_t From, std::size_t To, typename T1, typename T2, typename Func>
void transform(T1&& s, T2& t, Func f)
{
    transform<From>(std::forward<T1>(s), t, f, std::make_index_sequence<To-From+1>());
}

Demo.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!