[array] Support for multidimensional arrays?

Currently, creating a multidimensional boost/std::array is a bit unwieldy: array<array<array<int, 5>, 4>, 3> a; // int a[3][4][5] a[2][3][4] = 5; As a first step, we should consider enabling the element type T of array<T, N> to be an array type. array<int[4][5], 3> a; // int a[3][4][5] a[2][3][4] = 5; This almost works already, except for the array interface functions specified and implemented in terms of, say, std::equal or std::fill. It is an open question whether these standard algorithms should not be fixed themselves to work on array types, as swap_ranges has been, but for now, we can implement the array functions in a way that works. Opinions on that part? If we agree that this is a good idea, the next logical step would be to eliminate the special handling of the first extent and simply allow array<int[3][4][5]> a; // int a[3][4][5] a[2][3][4] = 5;

array<int, 3, 4, 5> would be my preferred syntax, because it works with variadic templates. The one main design decision for a multi-dimensional array is what begin() and end() iterate over: int, or array<int, 4, 5>? There was a standard proposal for this: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3794.html The decision that the author made in that paper was to have begin and end iterate over int in my example.

David Stone wrote:
The one main design decision for a multi-dimensional array is what begin() and end() iterate over: int, or array<int, 4, 5>?
They iterate over int[4][5] in my suggestion. This is, admittedly, not a "true" multidimensional array, it's just how C arrays work. int a[3][4][5]; for( auto& x: a ) { // x is int (&)[4][5] here } This is consistent with begin() returning T* == int(*)[4][5] and op[] returning T& == int(&)[4][5].

On 5/6/15, Peter Dimov <lists@pdimov.com> wrote:
Currently, creating a multidimensional boost/std::array is a bit unwieldy:
array<array<array<int, 5>, 4>, 3> a; // int a[3][4][5] a[2][3][4] = 5;
As a first step, we should consider enabling the element type T of array<T,
N> to be an array type.
array<int[4][5], 3> a; // int a[3][4][5] a[2][3][4] = 5;
This almost works already, except for the array interface functions specified and implemented in terms of, say, std::equal or std::fill.
It is an open question whether these standard algorithms should not be fixed
themselves to work on array types, as swap_ranges has been, but for now, we
can implement the array functions in a way that works.
Opinions on that part?
If we agree that this is a good idea, the next logical step would be to eliminate the special handling of the first extent and simply allow
array<int[3][4][5]> a; // int a[3][4][5] a[2][3][4] = 5;
There's a multidimensional array submitted for review at https://github.com/BrianJSmith/Array. The syntax is array<int, 3,4,5
, iteration is over each dimension, if that would suit your needs.
Brian -- www.maidsafe.net

On 05/06/2015 04:01 PM, Brian Smith wrote:
On 5/6/15, Peter Dimov <lists@pdimov.com> wrote:
Currently, creating a multidimensional boost/std::array is a bit unwieldy:
array<array<array<int, 5>, 4>, 3> a; // int a[3][4][5] a[2][3][4] = 5;
As a first step, we should consider enabling the element type T of array<T,
N> to be an array type.
array<int[4][5], 3> a; // int a[3][4][5] a[2][3][4] = 5;
This almost works already, except for the array interface functions specified and implemented in terms of, say, std::equal or std::fill.
It is an open question whether these standard algorithms should not be fixed
themselves to work on array types, as swap_ranges has been, but for now, we
can implement the array functions in a way that works.
Opinions on that part?
If we agree that this is a good idea, the next logical step would be to eliminate the special handling of the first extent and simply allow
array<int[3][4][5]> a; // int a[3][4][5] a[2][3][4] = 5;
There's a multidimensional array submitted for review at https://github.com/BrianJSmith/Array. The syntax is array<int, 3,4,5
, iteration is over each dimension, if that would suit your needs.
Brian
+1 I'm curious about this also. Larry
participants (4)
-
Brian Smith
-
David Stone
-
Larry Evans
-
Peter Dimov