问题
In the standard's specification for pointer arithmetic ([expr.add]/4.2, we have:
Otherwise, if
P
points to an array element i of an array objectx
with n elements ([dcl.array]), the expressionsP + J
andJ + P
(whereJ
has the value j) point to the (possibly-hypothetical) array element i + j ofx
if 0 ≤ i + j ≤ n and the expressionP - J
points to the (possibly-hypothetical) array element i − j ofx
if 0 ≤ i − j ≤ n.
What does "possibly-hypothetical" mean here? The passage already constrains the resulting pointer to be in range of the array. Well, including the one-past-the-end slot. Is that what it's referring to?
回答1:
Yes, it's the one-past-the-end "element".
[basic.compound]/3: [..] For purposes of pointer arithmetic ([expr.add]) and comparison ([expr.rel], [expr.eq]), 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 ofx
and an object of typeT
that is not an array element is considered to belong to an array with one element of typeT
. [..]
来源:https://stackoverflow.com/questions/59592182/what-does-possibly-hypothetical-mean-in-the-pointer-arithmetic-rules