cplusplus / CWG

Core Working Group
23 stars 7 forks source link

[basic.compound] p3 Define the term "the end of an object" #385

Closed xmh0511 closed 6 months ago

xmh0511 commented 1 year ago

Full name of submitter (unless configured in github; will be published with the issue): Jim X

[basic.compound] p3:

Every value of pointer type is one of the following:

  • [...]
  • a pointer past the end of an object ([expr.add]), or

[expr.add] does not introduce the term "the end of an object". So, this remains extra room for interpreting the term. An interpretation is: Is any pointer that was pointed to an object(subobject) a pointer past the end of the object after adding 1? Specifically:

int arr[2];
int* ptr = &arr[0];
ptr+=1; // #1

At #1, ptr points to the end of the subobject arr[0] and also is the start of the subobject arr[1]. Because a subobject is an object, since we didn't clear define the term "end of an object", so we can interpret it based on its literal meaning.

Suggested Resolution

Clearly define the term "a pointer past the end of an object", which may be:

a pointer past the end of the last element of an array x of n elements is considered to be equivalent to a pointer to a hypothetical array element n of x, the pointer is said past the end of x.

an object of type T that is not an array element is considered to ~belong to~ be an array with one element of type T.

The relevant issue is https://github.com/cplusplus/draft/issues/4784

languagelawyer commented 1 year ago

[expr.add] does not introduce the term "the end of an object". So, this remains extra room for interpreting the term.

Can't agree.

An interpretation is: Is any pointer that was pointed to an object(subobject) a pointer past the end of the object after adding 1?

Depends on the object.

At #1, ptr points to the end of the subobject arr[0] and also is the start of the subobject arr[1].

Per [expr.add], ptr just points to arr[1] and that's all.

Clearly define the term "a pointer past the end of an object"

The point is?

The relevant issue is https://github.com/cplusplus/draft/issues/4784

Does not look like an issue, at least the title.

xmh0511 commented 1 year ago

Per [expr.add], ptr just points to arr[1] and that's all.

So, a pointer points to arr[n] where arr is an array with n elements can also be said the pointer just points to arr[n] and that's all.

languagelawyer commented 1 year ago

A pointer to a hypothetical element is equivalent to a pointer past the end the last element, so it is https://timsong-cpp.github.io/cppwp/n4868/basic.compound#def:pointer_past_the_end_of in the pointer value taxonomy

xmh0511 commented 1 year ago

A pointer to a hypothetical element is equivalent to a pointer past the end the last element, so it is https://timsong-cpp.github.io/cppwp/n4868/basic.compound#def:pointer_past_the_end_of in the pointer value taxonomy

However, it does not mean we can conclude that a pointer to a hypothetical array element n of x is a pointer past then end of an object.

languagelawyer commented 1 year ago

«a pointer past the end of the last element of an array x of n elements is considered to be equivalent to a pointer to a hypothetical array element n of x»

xmh0511 commented 1 year ago

«a pointer past the end of the last element of an array x of n elements is considered to be equivalent to a pointer to a hypothetical array element n of x»

A is considered to B does not mean B is A. We said P+J points to the hypothetical element when P+J = n, in this sentence we didn't say a pointer points to the hypothetical element is a pointer past the end of arr[n-1], that is when you explicit say the pointer is what category then the pointer belongs to that category.

languagelawyer commented 1 year ago

Equivalence is a symmetric relation

xmh0511 commented 1 year ago

Equivalence is a symmetric relation

I think the strict equivalence relationship should be phrased as:

A pointer past the end of the last element of an array x of n elements is a pointer to a hypothetical array element n of x

Or:

A pointer to a hypothetical array element n of x is a pointer past the end of the last element of the array x of n elements

Then, the pointer points to the hypothetical array element is categorized as "a pointer past the end of an object"

xmh0511 commented 6 months ago

Since [expr.add] didn't define what "a pointer past the end of an object" means, instead, [expr.add] says "hypothetical element", so, a plausible suggestion is to change the second bullet of the pointer category

Every value of pointer type is one of the following:

  • [...]
  • a pointer to a hypothetical element of an array.

Then, we can define what "a pointer past the end of an object" means

A pointer to a hypothetical array element n of an array x of n elements is considered to be a pointer past the end of the last element of x

jensmaurer commented 6 months ago

[basic.compound] p3 already equates "past the end" with "hypothetical element" for purposes of pointer arithmetic, so I see no need to amend [expr.add]. No other hypothetical elements appear anywhere else in the text. And note that "pointer past the end" is simply a category name (like "blue" or"green") and, by itself, bears no particular meaning other than the properties ascribed to that name elsewhere in the text.

I understand there's a viewpoint that the definitions are backwards, but I'm not seeing any plausible misreading here; the suggestions offered don't feel like overall improvements.

xmh0511 commented 6 months ago

And note that "pointer past the end" is simply a category name (like "blue" or"green")

So, we should use this name consistently throughout the whole document, however, [expr.eq] p3.1 uses

past the last element of a different complete object

[expr.const] p14.3 uses

past the end of such an object

It seems to we should be coherent to use the wording "past the end of xxx".