This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 113d. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-03-20


1865. Pointer arithmetic and multi-level qualification conversions

Section: 7.6.6  [expr.add]     Status: CD4     Submitter: Geoffrey Romer     Date: 2014-02-15

[Moved to DR at the November, 2014 meeting.]

The resolution of issue 1504 added 7.6.6 [expr.add] paragraph 7:

For addition or subtraction, if the expressions P or Q have type “pointer to cv T”, where T is different from the cv-unqualified array element type, the behavior is undefined.

This wording was intended to address derived-base conversion in pointer arithmetic, but it inadvertently categorized as undefined behavior previously well-defined pointer arithmetic on pointers that are the result of multi-level qualification conversions. For example:

  void f() {
    int i = 0;
    int* arr[3] = {&i, &i, &i};
    int const * const * aptr = arr;
    assert(aptr[2] == &i);
  }

This now has undefined behavior because the type of *aptr is “pointer to const int,” which is different from the cv-unqualified array element type, “pointer to int.”

See also issue 330.

Proposed Resolution (July, 2014):

Change 7.6.6 [expr.add] paragraph 7 as follows:

For addition or subtraction, if the expressions P or Q have type “pointer to cv T”, where T is different from the cv-unqualified and the array element type are not similar (7.3.6 [conv.qual]), the behavior is undefined. [Note: In particular, a pointer to a base class cannot be used for pointer arithmetic when the array contains objects of a derived class type. —end note]