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

2022-07-16


1596. Non-array objects as array[1]

Section: 7.6.9  [expr.rel]     Status: CD4     Submitter: Daniel Krügler     Date: 2012-12-20

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

The provision to treat non-array objects as if they were array objects with a bound of 1 is given only for pointer arithmetic in C++ (7.6.6 [expr.add] paragraph 4). C99 supplies similar wording for the relational and equality operators, explicitly allowing pointers resulting from such implicit-array treatment to be compared. C++ should follow suit.

Proposed resolution (August, 2013):

  1. Change 7.6.2.2 [expr.unary.op] paragraph 3 as follows:

  2. ...Otherwise, if the type of the expression is T, the result has type “pointer to T” and is a prvalue that is the address of the designated object (6.7.1 [intro.memory]) or a pointer to the designated function. [Note: In particular, the address of an object of type “cv T” is “pointer to cv T”, with the same cv-qualification. —end note] For purposes of pointer arithmetic (7.6.6 [expr.add]) and comparison (7.6.9 [expr.rel], 7.6.10 [expr.eq]), an object that is not an array element whose address is taken in this way is considered to belong to an array with one element of type T. [Example:

      struct A { int i; };
      struct B : A { };
      ... &B::i ...        // has type int A::*
      int a;
      int* p1 = &a;
      int* p2 = p1 + 1;    // Defined behavior
      bool b = p2 > p1;    // Defined behavior, with value true
    

    end example] [Note: a pointer to member...

  3. Delete 7.6.6 [expr.add] paragraph 4:

  4. For the purposes of these operators, a pointer to a nonarray object behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type.
  5. Change 7.6.6 [expr.add] paragraph 5 as follows:

  6. When an expression that has integral type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object [Footnote: An object that is not an array element is considered to belong to a single-element array for this purpose; see 7.6.2.2 [expr.unary.op] —end footnote], and the array is large enough, the result points to an element...
  7. Change 7.6.9 [expr.rel] paragraph 3 as follows:

  8. Comparing pointers to objects [Footnote: An object that is not an array element is considered to belong to a single-element array for this purpose; see 7.6.2.2 [expr.unary.op] —end footnote] is defined as follows:...

[Drafting note: No change is proposed for 7.6.10 [expr.eq], since the comparison is phrased in terms of “same address”, not in terms of array elements, so the handling of one-past-the-end addresses falls out of the specification of pointer arithmetic.]