cplusplus / CWG

Core Working Group
23 stars 7 forks source link

[dcl.array] Clarify "numbered `0` to `N-1`" #556

Open Eisenwave opened 2 weeks ago

Eisenwave commented 2 weeks ago

Reference (section label): [dcl.array]

Discussion (Mattermost): https://chat.isocpp.org/general/pl/3gzbcx4fy3dgjpyno7hkejuksa

Issue description

[dcl.array] p6 states:

An object of type “array of N U” consists of a contiguously allocated non-empty set of N subobjects of type U, known as the elements of the array, and numbered 0 to N-1.

It is not sufficiently clear what "numbered 0 to N-1" means. [basic.compound] Note 5 states that an array has the same address as its first element, relying on this sentence, but it's not clear from normative wording that the first element is found at offset 0 and the last element is found at offset N-1.

In general, "numbered" doesn't strictly imply an ordering from first to last, "to be numbered" has no defined meaning, and the use of "set" instead of "sequence" implies that no particular order is enforced.

Suggested resolution

Update [dcl.array] p6 as follows:

An object
+a
of type “array of N U” consists of a contiguously allocated non-empty
-set
+sequence
of N subobjects of type U, known as the elements of the array
-, and numbered 0 to N-1.
+. The elements of the array, from first to last,
+may be accessed using the expressions a[0] through a[N-1] ([expr.sub]).
Eisenwave commented 2 weeks ago

To be fair, I'm not sure if this is just kicking the can down the road, given that "first" isn't really defined either.

Another thing we could do is to say in [conv.array] that the converted pointer represents the address of the array.

jensmaurer commented 2 weeks ago

I think the change set -> sequence is good, and implies ordering. Everything else seems not an improvement. In particular, a[n] is interpreted as pointer arithmetic, which seems a bit indirect for such a basic definition.

t3nsor commented 2 weeks ago

I like "set" -> "sequence" but I still don't know if it's clear enough what exactly the implied ordering is. We have a sequence of elements 0 to N - 1, so the first one is 0 and the last one is N - 1, but first in what sense?

I think what we mean is that the ordering of the elements is the same as the ordering of the bytes occupied by those elements in the object representation of the array.

jensmaurer commented 2 weeks ago

Shouldn't that latter statement be implied by a (currently lacking) consistency requirement between pointer (= address) comparisons on the object representation vs. the elements of the array?

Eisenwave commented 2 weeks ago

Ok, new idea:

An object of type “array of N U” consists of a contiguously allocated non-empty
-set
+sequence
of N subobjects of type U, known as the elements of the array
-, and numbered 0 to N-1.
+. An array has the same address as its first element.
+The storage of any element
+is adjacent to the storage of its preceding element (if any).

Ultimately, we want [basic.compound] Note 5 to be obviously correct, and outright stating that the first element has the same address is a really good way to do so.

We don't need to talk about the object representation I think; we can also talk about where the elements are located within the storage of the surrounding array. That seems equally valid.