使用额外元素扩展std :: array的每个std :: tuple

时间:2017-07-25 08:09:39

标签: c++ c++14

我有一个facet类型,基本上代表一个元组数组:

template <typename... Args>
struct facet {
    static constexpr std::size_t npoints = sizeof...(Args);
    using point_type = std::tuple<Args...>;
    using array_type = std::array<point_type, npoints>;

    facet(point_type, point_type); // imagine facet(point_type... )
};

构造函数在这里简化为2个参数,但想象一个构造函数可以采用类型为point_type的n参数。

我有另一个包含facet

向量的类
template <typename R, typename... Args>
struct X {

    using facet_type = facet<Args..., R>;

    using const_facet_type =
        std::pair<std::array<typename facet<Args...>::point_type,
                             facet<Args..., R>::npoints>, R>;

    // vector of facets
    std::vector<facet_type> facets_;

    X(std::vector<facet_type>);

    X(std::vector<const_facet_type> facets) {
        facets_.reserve(facets.size());
        add_facets(std::move(facets),
                   std::make_index_sequence<facet_type::npoints>{},
                   std::make_index_sequence<sizeof...(Args)>{});
    }

    template <std::size_t... Is, std::size_t... Us>
    void add_facets(std::vector<const_facet_type> &&facets,
                    std::index_sequence<Is...>,
                    std::index_sequence<Us...>) {
        for (auto &&p: facets) {
            facets_.emplace_back(
                    std::make_tuple(
                        std::get<Us>(std::get<Is>(p.first))..., p.second)...);
        }
    }

};

问题出在第二个构造函数中 - 这个构造函数不采用facet的向量,而是采用对数组/ R的向量,其中数组中的每个点(元组)包含N - 1个元素( sizeof... (Args))。

我的目标是能够构建X的实例,如下所示:

X<double, double> x({
    {{0.0, 5.0}, 10.0},
    {{5.0, 8.0}, 12.0}
});

// which would be equivalent (using the first constructor)
X<double, double> x({
    {{0.0, 10.0}, {5.0, 10.0}},
    {{5.0, 12.0}, {8.0. 12.0}}
});

问题出在这行代码中:

facets_.emplace_back(
    std::make_tuple(
        std::get<Us>(std::get<Is>(p.first))..., p.second)...);
//                                         ^^^ Us        ^^^ Is

因为我需要扩展UsIs(&#34;交叉&#34; - 实际上扩展它们),这是不可能的。

我可以手动构建一个数组,然后展开它,但是我想知道是否有办法在没有额外数组的情况下完成它?

1 个答案:

答案 0 :(得分:2)

如果我理解正确,并且如果可能的话,您可以将序列分组为仅一个,例如:

X(std::vector<const_facet_type> facets) {
    facets_.reserve(facets.size());
    add_facets(std::move(facets),
               std::make_index_sequence<facet_type::npoints * sizeof...(Args)>{});
}

template <std::size_t... Is>
void add_facets(std::vector<const_facet_type> &&facets,
                std::index_sequence<Is...>) {
    constexpr auto size = sizeof...(Args);
    for (auto &&p: facets) {
        facets_.emplace_back(
                std::make_tuple(
                    std::get<Is % size>(std::get<Is / size>(p.first)),
                    p.second)...);
    }
}
相关问题