根据两个tuple生成一个笛卡尔乘积的tuple类型

需求

给定两个tuple, tuple<A, B>和tuple<C, D>,生成一个笛卡尔积的tuple,即tuple<pair<A,C>, pair<A,D>, pair<B, C>, pair<B,D>>

注意:这两个tuple中的参数是变参,参数个数不一定相等。

请大家给出实现代码,看谁实现得最好,期待精妙的代码。

《根据两个tuple生成一个笛卡尔乘积的tuple类型》有10个想法

  1. 我也发一下我的实现:

  2. template<std::size_t I = 0, typename TElement, typename Tuple2, typename Result, class =
    typename std::enable_if<I == std::tuple_size::value, Result>::type, class = void>
    auto EMulTuple(TElement const& t1_element, Tuple2& tuple2, Result const & result)
    {
    return Result(result);
    }

    template<std::size_t I = 0, typename TElement, typename Tuple2, typename Result, class =
    typename std::enable_if<I != std::tuple_size::value, void>::type>
    auto EMulTuple(TElement const& t1_element, Tuple2& tuple2, Result const & result)
    {
    return EMulTuple(t1_element, tuple2, std::tuple_cat(result, std::make_tuple(make_pair(t1_element, std::get(tuple2)))));
    }

    template<std::size_t I = 0, typename Tuple1, typename Tuple2, typename Result, class =
    typename std::enable_if<I == std::tuple_size::value, Result>::type, class = void>
    auto DecareTuple(Tuple1& tuple1, Tuple2& tuple2, Result const & result)
    {
    return Result(result);
    }

    template<std::size_t I = 0, typename Tuple1, typename Tuple2, typename Result, class =
    typename std::enable_if<I != std::tuple_size::value, void>::type>
    auto DecareTuple(Tuple1& tuple1, Tuple2& tuple2, Result const & result)
    {
    auto result_row = EMulTuple(std::get
    (tuple1), tuple2, result);
    return DecareTuple
    (tuple1, tuple2, result_row);
    }

发表评论