排列参数包中的参数
Permute the parameters in a parameter pack
我有两个结构
template <int ... values>
struct foo {}
template <int ... values>
struct lists {}
我想要一个函数 bar
接受 lists
和 foo
并将 foo
的参数置换为 return 另一个 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;
我有两个结构
template <int ... values>
struct foo {}
template <int ... values>
struct lists {}
我想要一个函数 bar
接受 lists
和 foo
并将 foo
的参数置换为 return 另一个 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;