From Jason Turner

[expr.add]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpm67gc0y1/{from.md → to.md} +15 -4
tmp/tmpm67gc0y1/{from.md → to.md} RENAMED
@@ -39,26 +39,37 @@ from an expression `P` of pointer type, the result has the type of `P`.
39
  (possibly-hypothetical) array element i + j of `x` if 0 ≤ i + j ≤ n
40
  and the expression `P - J` points to the (possibly-hypothetical) array
41
  element i - j of `x` if 0 ≤ i - j ≤ n.
42
  - Otherwise, the behavior is undefined.
43
 
 
 
 
 
44
  When two pointer expressions `P` and `Q` are subtracted, the type of the
45
  result is an *implementation-defined* signed integral type; this type
46
  shall be the same type that is defined as `std::ptrdiff_t` in the
47
  `<cstddef>` header [[support.types.layout]].
48
 
49
  - If `P` and `Q` both evaluate to null pointer values, the result is 0.
50
  - Otherwise, if `P` and `Q` point to, respectively, array elements i and
51
  j of the same array object `x`, the expression `P - Q` has the value
52
  i - j.
53
- - Otherwise, the behavior is undefined. \[*Note 1*: If the value i - j
54
  is not in the range of representable values of type `std::ptrdiff_t`,
55
  the behavior is undefined. — *end note*]
56
 
57
  For addition or subtraction, if the expressions `P` or `Q` have type
58
  “pointer to cv `T`”, where `T` and the array element type are not
59
  similar [[conv.qual]], the behavior is undefined.
60
 
61
- [*Note 2*: In particular, a pointer to a base class cannot be used for
62
- pointer arithmetic when the array contains objects of a derived class
63
- type. — *end note*]
 
 
 
 
 
 
 
64
 
 
39
  (possibly-hypothetical) array element i + j of `x` if 0 ≤ i + j ≤ n
40
  and the expression `P - J` points to the (possibly-hypothetical) array
41
  element i - j of `x` if 0 ≤ i - j ≤ n.
42
  - Otherwise, the behavior is undefined.
43
 
44
+ [*Note 1*: Adding a value other than 0 or 1 to a pointer to a base
45
+ class subobject, a member subobject, or a complete object results in
46
+ undefined behavior. — *end note*]
47
+
48
  When two pointer expressions `P` and `Q` are subtracted, the type of the
49
  result is an *implementation-defined* signed integral type; this type
50
  shall be the same type that is defined as `std::ptrdiff_t` in the
51
  `<cstddef>` header [[support.types.layout]].
52
 
53
  - If `P` and `Q` both evaluate to null pointer values, the result is 0.
54
  - Otherwise, if `P` and `Q` point to, respectively, array elements i and
55
  j of the same array object `x`, the expression `P - Q` has the value
56
  i - j.
57
+ - Otherwise, the behavior is undefined. \[*Note 2*: If the value i - j
58
  is not in the range of representable values of type `std::ptrdiff_t`,
59
  the behavior is undefined. — *end note*]
60
 
61
  For addition or subtraction, if the expressions `P` or `Q` have type
62
  “pointer to cv `T`”, where `T` and the array element type are not
63
  similar [[conv.qual]], the behavior is undefined.
64
 
65
+ [*Example 1*:
66
+
67
+ ``` cpp
68
+ int arr[5] = {1, 2, 3, 4, 5};
69
+ unsigned int *p = reinterpret_cast<unsigned int*>(arr + 1);
70
+ unsigned int k = *p; // OK, value of k is 2[conv.lval]
71
+ unsigned int *q = p + 1; // undefined behavior: p points to an int, not an unsigned int object
72
+ ```
73
+
74
+ — *end example*]
75