我想编写一个constexpr模板函数来置换作为参数传入的数组元素.所以我想出了这样的事情:
template <typename T, std::size_t N, typename... Ts>
constexpr std::array<T, N> permute(const std::array<T, N>& arr, const std::array<int, N>& permutation, Ts&&... processed)
{
return (sizeof...(Ts) == N) ?
std::array<T, N>{ std::forward<Ts>(processed)... } :
permute(arr, permutation, std::forward<Ts>(processed)..., arr[permutation[sizeof...(Ts)]]);
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
constexpr std::array<int, 3> arr{ 1, 2, 3 };
constexpr std::array<int, 3> permutation{ 2, 1, 0 };
constexpr auto result = permute(arr, permutation); //result should contain { 3, 2, 1 }
Run Code Online (Sandbox Code Playgroud)
问题是上面的代码没有编译.出于某种原因,g ++ 6.4尝试在"已处理"模板参数包下隐藏4个或更多参数来实例化置换模板.你能帮我纠正我的代码并让它编译吗?