I have and std::array of std::array, and say that I want to initialize all the arrays to {1,2,3}. We write:
std::array<std::array<int,3>,3> x{{1,2,3},{1,2,3},{1,2,3}};
This is not very handy. It becomes really messy when you have more than 3 arrays, or each array has more than 3 elements.
However, it becomes even worse if the size of the array is not known a priori:
template <size_t n, size_t T> struct foo{
std::array<std::array<int,n>,T> x;
}
How can you initialize x? To make it clearer, I would like to initialize all the arrays in x to an array of a certain parameter that is given. That is, something like:
template <size_t n, size_t T> struct foo{
static constexpr int N{20};
std::array<std::array<int,n>,T> x;
foo() : x{ {N,N,...}, {N,N,...}, ...} {}
}
(If that were possible). Any suggestion or ideas? I can always iterate through x and call the method fill, as in the following piece of code:
for (size_t idx = 0; idx < x[0].size(); idx++)
x[idx].fill(N);
But that is not initialization, right? I am new to using std::array and I do not know whether I am asking something dummy here :/
std::array<std::array<int,3>,3> x{}will make them all0's if you are okay with that.fooknow what it should initialize the elements to? Should this be delegated to the caller? You're sort-of implying you know what the values should be, but don't actually say.0. I will change the question.int,bool,double,...). No tricks here ;)