Title
array<array<int, 3>, 4> should be layout-compatible with int[4][3]
Status
nad
Section
[array]
Submitter
Jeffrey Yasskin

Created on 2013-10-04.00:00:00 last changed 50 months ago

Messages

Date: 2020-11-09.20:22:58
The desire to use

Rationale:

std::array like this seems like an "XY problem". The goal should be "replace C arrays" not "replace C arrays with std::array", because std::array is not suitable here. There are superior solutions being proposed, and will be available in a future version of C++ (e.g. using mdspan as a multi-dimensional view on an array).
Date: 2020-11-09.00:00:00

[ 2020-11-09 Status changed: Tentatively NAD → NAD. ]

Date: 2020-02-15.00:00:00

[ 2020-02-14, Prague ]

LWG discussions and decision for NAD.

Date: 2013-10-04.00:00:00

In order to replace some uses of C arrays with std::array, we need it to be possible to cast from a std::array<> to an equivalent C array. Core wording doesn't appear to be in quite the right state to allow casting, but if we specify that appropriate types are layout-compatible, we can at least write:

union {
  array<array<array<int, 2>, 3>, 4> arr;
  int carr[4][3][2];
};

to view memory as the other type: C++14 CD [class.mem]p18.

I believe it's sufficient to add "array<T, N> shall be layout-compatible ([basic.types]) with T[N]." to [array.overview], but we might also need some extension to [class.mem] to address the possibility of layout-compatibility between struct and array types.

I checked that libc++ on MacOS already implements this, although it would be good for someone else to double-check; I haven't checked any other standard libraries.

History
Date User Action Args
2020-11-09 20:22:58adminsetmessages: + msg11533
2020-02-16 07:31:32adminsetmessages: + msg11133
2020-02-16 07:31:32adminsetmessages: + msg11132
2020-02-16 07:31:32adminsetstatus: new -> nad
2013-10-04 00:00:00admincreate