This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of New status.

2335. array<array<int, 3>, 4> should be layout-compatible with int[4][3]

Section: 22.3.7 [array] Status: New Submitter: Jeffrey Yasskin Opened: 2013-10-04 Last modified: 2016-02-10

Priority: 3

View all other issues in [array].

View all issues with New status.


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 [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.

Proposed resolution: