我想编写一个constexpr模板函数,用于对作为参数传递的数组元素进行排列。因此,我想到了以下代码:
问题在于上述代码无法编译。由于某种原因,g++ 6.4试图在'processed'模板参数包下隐藏的四个或更多参数中实例化permute模板。你能帮我修改代码并使其编译吗?
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)]]);
}
使用示例:
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 }
问题在于上述代码无法编译。由于某种原因,g++ 6.4试图在'processed'模板参数包下隐藏的四个或更多参数中实例化permute模板。你能帮我修改代码并使其编译吗?