17. Creating a 2D array with basic operations

Before looking at how we could define such a structure, let's consider several test cases for it. The following snippet shows all the functionality that was requested:

int main(){   // element access   array2d<int, 2, 3> a {1, 2, 3, 4, 5, 6};   for (size_t i = 0; i < a.size(1); ++i)      for (size_t j = 0; j < a.size(2); ++j)      a(i, j) *= 2;   // iterating   std::copy(std::begin(a), std::end(a),       std::ostream_iterator<int>(std::cout, " "));    // filling    array2d<int, 2, 3> b;   b.fill(1);   // swapping   a.swap(b);   // moving   array2d<int, 2, 3> c(std::move(b));}

Note that for element access, we are using operator(), such as in a(i,j), and not operator[], such as in a[i][j], because only the former can take multiple arguments ...

Get The Modern C++ Challenge now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.