I'd like to write a constexpr template function that permutes elements of an array passed in as a parameter. So I've come up with something like this:
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)]]);
}
Usage example:
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 }
The problem is the above code doesn't compile. For some reason g++ 6.4 tries to instantiate the permute template with 4 and more parameters hidden under 'processed' template parameter pack. Can you help me correct my code and make it compile?
Aucun commentaire:
Enregistrer un commentaire