我有两个结构
template <int ... values>
struct foo {}
template <int ... values>
struct lists {}
我想要一个函数 bar
这需要 lists
和 foo
并排列 foo
的参数返回另一个 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>
的另一个版本就优势而言,这就是元素的排列方式。我该怎么做?
谢谢
最佳答案
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;
关于c++ - 排列参数包中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37287695/