'Reference over array into array of reference

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>>.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source