2016-05-17 21 views
0

我有兩個結構置換的參數組的參數

template <int ... values> 
struct foo {} 

template <int ... values> 
struct lists {} 

我想有一個功能bar,它利用foolistsfoo和的置換參數,返回另一個foo

another_foo bar(lists<0,3,1,2>, foo<4,5,6,7>) 

我希望another_foo的類型爲foo<4,7,5,6>,所以基本上按照另一個參數來排序一個參數。我也希望函數任意參數列表不一定爲索引工作foo參數,如

another_foo bar(lists<20,12,21,13>, foo<4,5,6,7>) 

我想another_foo是類型別名foo<6,4,7,5>。在這種情況下,lists<20,12,21,13>基本上是lists<2,0,3,1>的另一個版本,就如何排列元素的優勢一樣。我怎樣才能做到這一點?

感謝

+0

其相同的規則,對無論很多爭論,如'欄(名單<1,2,0>,富<3,2,5>)如何'應該返回'富<2,5,3>'。當然,異常是空的'list'和'lists',你顯然不能在其中排列一個元素 –

回答

2
template<size_t N> 
constexpr size_t count_less(const int (&seq)[N], int i, size_t cur = 0) { 
    return cur == N ? 0 
        : (count_less(seq, i, cur + 1) + (seq[cur] < i ? 1 : 0)); 
} 

template<class List, class Foo, class Seq> 
struct meow; 

template<int... ls, int... fs, size_t... ss> 
struct meow<lists<ls...>, foo<fs...>, std::index_sequence<ss...>>{ 
    constexpr static int lst[] = { ls... }; 
    constexpr static int fvals[] = {fs...}; 
    using type = foo<fvals[count_less(lst, lst[ss])]...>; 
}; 

template<int... ls, int... fs> 
auto bar(lists<ls...>, foo<fs...>) 
    -> typename meow<lists<ls...>, foo<fs...>, 
        std::make_index_sequence<sizeof...(ls)>>::type; 
+0

當我嘗試你的代碼時,我會出現越界錯誤。這裏是[demo](http://coliru.stacked-crooked.com/a/6faf12f6905c76bd) –

+0

@TasamFarkie錯字,應該是'<'。固定。 –

+0

想通了,謝謝。 –