Reputation: 3612
I have an array of some elements. I would like to create a function that will generate an array of reference_wrapper containing the references of the element.
Something that will have an interface like:
template<typename T, size_t N>
std::array<std::reference_wrapper<T>,N> wrap(std::array<T,N>& a);
What would be the best way to do it?
Note: I don't want to use vector.
Upvotes: 1
Views: 75
Reputation: 48978
You can do it like this. Here's a demo.
namespace detail {
template<typename T, std::size_t N, std::size_t... Index>
std::array<std::reference_wrapper<T>, N> wrap(std::array<T, N>& a,
std::index_sequence<Index...>) {
return {a[Index]...};
}
}
template<typename T, size_t N>
std::array<std::reference_wrapper<T>,N> wrap(std::array<T,N>& a) {
return detail::wrap(a, std::make_index_sequence<N>());
}
Upvotes: 8