2017-07-25 41 views
0

我有一個類型facet基本上代表元組的數組:延長每個的std ::一個std陣列的::元組與一個額外的元素

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)...); 
     } 
    } 

}; 

的問題是在第二構造 - 此構造不採取面的向量,而是對數組/ 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 

因爲我需要在這兩個Us擴大和Is(「交叉」 - 實際擴大它們),這是不可能的。

我可以手動構建一個數組然後展開它,但是我想知道是否有辦法在沒有額外數組的情況下做到這一點?

回答

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)...); 
    } 
} 
+0

某種類型的巫術的右邊有...... :)你只是倒'是/ size'和'是%尺寸「,但否則這很好,謝謝! – Holt

相關問題