I have an array std::array<T, N> arr for some T, N and I'd like to get an array of reference over arr's elements like so std::array<std::reference_wrapper<T>, N> arr_ref.
But as a reference needs to be set at its initialization, I did not work out a solution. Therefore I would like to do something like that:
std::array<std::reference_wrapper<T>, N> ref{}
for (std::size_t i{0}; i < N; ++i)
ref[i] = arr[i];
But at compile-time and at the initialization of ref.
I thought of using some variadic template magic to convert my initial array to a parameter pack and then take a reference to each element but I am not sure this is possible.
My last option would be an array of raw ptrs or of std::optional<std::reference_wrapper<T>>.
auto& ref = arr;)? What is the actual and underlying problem your individual references is supposed to solve?std::spanstd::span(already mentioned, with Boost span as an alternative) might be useful. Or the ranges library (or the range library the standard ranges is based on).