array<array<int, 3>, 4>
should be layout-compatible with int[4][3]
Section: 23.3.3 [array] Status: NAD Submitter: Jeffrey Yasskin Opened: 2013-10-04 Last modified: 2020-11-09
Priority: 3
View all other issues in [array].
View all issues with NAD status.
Discussion:
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 (6.8 [basic.types]) with T[N]
." to
23.3.3.1 [array.overview], but we might also need some extension to
11.4 [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.
[2020-02-14, Prague]
LWG discussions and decision for NAD.
[2020-11-09 Status changed: Tentatively NAD → NAD.]
Rationale:
The desire to usestd::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).
Proposed resolution: