From Jason Turner

[expr.add]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpfzq0__uv/{from.md → to.md} +26 -48
tmp/tmpfzq0__uv/{from.md → to.md} RENAMED
@@ -26,61 +26,39 @@ For subtraction, one of the following shall hold:
26
 
27
  The result of the binary `+` operator is the sum of the operands. The
28
  result of the binary `-` operator is the difference resulting from the
29
  subtraction of the second operand from the first.
30
 
31
- For the purposes of these operators, a pointer to a nonarray object
32
- behaves the same as a pointer to the first element of an array of length
33
- one with the type of the object as its element type.
34
-
35
  When an expression that has integral type is added to or subtracted from
36
  a pointer, the result has the type of the pointer operand. If the
37
- pointer operand points to an element of an array object, and the array
38
- is large enough, the result points to an element offset from the
39
- original element such that the difference of the subscripts of the
40
- resulting and original array elements equals the integral expression. In
41
- other words, if the expression `P` points to the i-th element of an
42
- array object, the expressions `(P)+N` (equivalently, `N+(P)`) and
43
- `(P)-N` (where `N` has the value n) point to, respectively, the i+n-th
44
- and i-n-th elements of the array object, provided they exist. Moreover,
45
- if the expression `P` points to the last element of an array object, the
46
- expression `(P)+1` points one past the last element of the array object,
47
- and if the expression `Q` points one past the last element of an array
48
- object, the expression `(Q)-1` points to the last element of the array
49
- object. If both the pointer operand and the result point to elements of
50
- the same array object, or one past the last element of the array object,
51
- the evaluation shall not produce an overflow; otherwise, the behavior is
52
- undefined.
53
 
54
  When two pointers to elements of the same array object are subtracted,
55
- the result is the difference of the subscripts of the two array
56
- elements. The type of the result is an *implementation-defined* signed
57
- integral type; this type shall be the same type that is defined as
58
- `std::ptrdiff_t` in the `<cstddef>` header ([[support.types]]). As with
59
- any other arithmetic overflow, if the result does not fit in the space
60
- provided, the behavior is undefined. In other words, if the expressions
61
- `P` and `Q` point to, respectively, the i-th and j-th elements of an
62
- array object, the expression `(P)-(Q)` has the value i-j provided the
63
- value fits in an object of type `std::ptrdiff_t`. Moreover, if the
64
- expression `P` points either to an element of an array object or one
65
- past the last element of an array object, and the expression `Q` points
66
- to the last element of the same array object, the expression
67
- `((Q)+1)-(P)` has the same value as `((Q)-(P))+1` and as
68
- `-((P)-((Q)+1))`, and has the value zero if the expression `P` points
69
- one past the last element of the array object, even though the
70
- expression `(Q)+1` does not point to an element of the array object.
71
- Unless both pointers point to elements of the same array object, or one
72
- past the last element of the array object, the behavior is
73
- undefined.[^26]
74
 
75
  For addition or subtraction, if the expressions `P` or `Q` have type
76
- “pointer to cv `T`”, where `T` is different from the cv-unqualified
77
- array element type, the behavior is undefined. In particular, a pointer
78
- to a base class cannot be used for pointer arithmetic when the array
79
- contains objects of a derived class type.
80
 
81
- If the value 0 is added to or subtracted from a pointer value, the
82
- result compares equal to the original pointer value. If two pointers
83
- point to the same object or both point one past the end of the same
84
- array or both are null, and the two pointers are subtracted, the result
85
- compares equal to the value 0 converted to the type `std::ptrdiff_t`.
 
 
 
86
 
 
26
 
27
  The result of the binary `+` operator is the sum of the operands. The
28
  result of the binary `-` operator is the difference resulting from the
29
  subtraction of the second operand from the first.
30
 
 
 
 
 
31
  When an expression that has integral type is added to or subtracted from
32
  a pointer, the result has the type of the pointer operand. If the
33
+ expression `P` points to element x[i] of an array object `x` with n
34
+ elements, [^25] the expressions `P + J` and `J + P` (where `J` has the
35
+ value j) point to the (possibly-hypothetical) element x[i + j] if
36
+ 0 i + j n; otherwise, the behavior is undefined. Likewise, the
37
+ expression `P - J` points to the (possibly-hypothetical) element
38
+ x[i - j] if 0 ≤ i - j ≤ n; otherwise, the behavior is undefined.
 
 
 
 
 
 
 
 
 
 
39
 
40
  When two pointers to elements of the same array object are subtracted,
41
+ the type of the result is an *implementation-defined* signed integral
42
+ type; this type shall be the same type that is defined as
43
+ `std::ptrdiff_t` in the `<cstddef>` header ([[support.types]]). If the
44
+ expressions `P` and `Q` point to, respectively, elements x[i] and x[j]
45
+ of the same array object `x`, the expression `P - Q` has the value
46
+ i - j; otherwise, the behavior is undefined.
47
+
48
+ [*Note 1*: If the value i - j is not in the range of representable
49
+ values of type `std::ptrdiff_t`, the behavior is
50
+ undefined. *end note*]
 
 
 
 
 
 
 
 
 
51
 
52
  For addition or subtraction, if the expressions `P` or `Q` have type
53
+ “pointer to cv `T`”, where `T` and the array element type are not
54
+ similar ([[conv.qual]]), the behavior is undefined.
 
 
55
 
56
+ [*Note 2*: In particular, a pointer to a base class cannot be used for
57
+ pointer arithmetic when the array contains objects of a derived class
58
+ type. *end note*]
59
+
60
+ If the value 0 is added to or subtracted from a null pointer value, the
61
+ result is a null pointer value. If two null pointer values are
62
+ subtracted, the result compares equal to the value 0 converted to the
63
+ type `std::ptrdiff_t`.
64