We can’t create an array of references as they are not objects. For example, following would not work as expected:
int a{1};
int b{2};
int &a_ref{a};
int &b_ref{b};
std::array<int, 2> arr{a_ref, b_ref};arr contains values of a and b, but not references to them.
If we really want to create an array of references, we can use std::reference_wrapper. For example:
#include <iostream>
#include <array>
#include <functional>
template <typename T, auto N>
void printArray(const std::array<T, N> &arr)
{
for (auto ele : arr)
{
std::cout << ele << " ";
}
std::cout << '\n';
}
int main(int argc, char const *argv[])
{
int x{1};
int y{2};
std::array<std::reference_wrapper<int>, 2> arr{x, y};
printArray(arr);
arr[0].get() = 10;
printArray(arr);
std::cout << x << '\n';
return 0;
}