【发布时间】:2014-08-01 10:23:44
【问题描述】:
我试图在编译时交换可变参数模板的两个参数:
template<int...Numbers>struct sequence{};
template<size_t first,size_t second>
struct Swap_Pair
{
const static size_t First = first;
const static size_t Second = second;
};
template <int...Numbers,class swap_pair>
struct Swap_Data
{
static std::array<int, sizeof...(Numbers)> data_;//How to swap Numbers base on the pair and store it in data_ ?
};
用例应该是:
sequence<1, 2, 3, 4, 5, 6> array;
auto result = Swap_Data < array, Swap_Pair<2, 5> > ::data_;
//result is now std::array which contains 1 2 6 4 5 3
我不知道写Swap_Data 的正确方法是什么。
如何进行递归交换以在编译时交换可变参数并转换为 std::array ?
【问题讨论】:
-
类似
int swapped_arr[] = { unswapped_arr[N == swap_pair::First ? swap_pair::Second : N == swap_pair::Second ? swap_pair::First : N]... };的地方有一个未交换元素的数组;你也可以使用一些integer_sequence和get<N>(seq)函数。
标签: c++ templates c++11 metaprogramming variadic-templates