Skip to content

[expr.add] Undefined behavior in pointer subtraction has been removed accidentally #922

Description

@eisenwave

Subclause: [expr.add]

Issue description

P3899R3 accidentally removed undefined behavior in the case where the result of subtracting pointers is not representable as std::ptrdiff_t. This was caused by limiting undefined behavior in subtraction to arithmetic types, which pointers are not.

Proposed resolution

Make the note in [expr.add] paragraph 5 normative text:

 The result of subtracting two pointer expressions P and Q
 is a prvalue of type std​::​ptrdiff_t ([support.types.layout]).
     (5.1) If P and Q both evaluate to null pointer values,
     the value is 0.
     (5.2) Otherwise, if P and Q point to, respectively,
     array elements i and j of the same array object x,
     the expression P - Q has the value i−j.
-    [Note 2:
     If the value i−j is not in the range of representable values
     of type std​::​ptrdiff_t, the behavior is undefined
-    ([expr.pre])
     .
-    — end note]
     (5.3) Otherwise, the behavior is undefined.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions