Title
Address comparison between different member subobjects
Status
nad
Section
7.6.10 [expr.eq]
Submitter
Daveed Vandevoorde

Created on 2016-04-29.00:00:00 last changed 74 months ago

Messages

Date: 2017-07-15.00:00:00

Rationale (July, 2017):

CWG determined that the existing wording is correct: the result of the comparison is implementation-defined, but not unspecified, so the program is well-formed but the assertion is not guaranteed to pass.

Date: 2022-11-20.07:54:16

According to 7.6.10 [expr.eq] bullet 2.1,

Comparing pointers is defined as follows:

  • If one pointer represents the address of a complete object, and another pointer represents the address one past the last element of a different complete object87, the result of the comparison is unspecified.

The use of the term “complete object” is confusing. A complete object is one that is not a subobject of any other object (6.7.2 [intro.object] paragraph 2), so this restriction apparently does not apply to non-static data members. Is the following guaranteed to work?

  struct S {
    int i[2];
    int j[2];
  };

  constexpr bool check1() {
    S s = { { 1, 2 }, { 3, 4 } };
    return &s.i[2] == &s.j[0];
  }

  static_assert(check1(), "Guaranteed?");

In particular, is there a guarantee that there is no padding between nonstatic data members of the same type?

History
Date User Action Args
2018-02-27 00:00:00adminsetmessages: + msg6008
2016-04-29 00:00:00admincreate