================
@@ -1543,3 +1544,55 @@ namespace GH197403 {
   }
   static_assert(test() == 0);
 }
+
+namespace GH223064 {
+  struct A {
+    int n;
+  };
+  struct B : A {} b[2];
+
+  constexpr int *base_array_element() {
+    A *p = b;
+    return &static_cast<B *>(p)[1].n;
+  }
+  static_assert(base_array_element() == &b[1].n);
+
+  constexpr int pointer_difference() {
+    A *p = b;
+    return p - p;
+  }
+  static_assert(pointer_difference() == 0);
----------------
adiroy28852 wrote:

What i understood, was, 

`&b[1]` and `&b[0]` converted to  `A*` point at the `A` base sub-objects of two 
different `B` elements. These aren't elements of the same `A` array, so `p - q` 
shouldn't be a constant expression, and clang should reject it. Kindly correct 
if wrong. 

would below be correct ?

```cpp
constexpr auto difference(A *p, A *q) {
  return p - q; // expected-note {{subtracted pointers are not elements of the 
same array}}
}
static_assert(difference(&b[1], &b[0]) == 1); // expected-error {{static 
assertion expression is not an integral constant expression}} \
                                              // expected-note {{in call to 
'difference(&b[1], &b[0])'}}
```

Something like this?

https://github.com/llvm/llvm-project/pull/230599
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to