Bug 103552 - Compile-time comparison of subobject and parent class
Summary: Compile-time comparison of subobject and parent class
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2021-12-04 13:25 UTC by Fedor Chelnokov
Modified: 2025-03-17 22:02 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2025-03-17 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fedor Chelnokov 2021-12-04 13:25:03 UTC
This program:
```
struct A{};
struct B : A { A a; };

int main() {
    constexpr B b{};
    static_assert( &static_cast<const A&>(b) < &b.a );
}
```
is accepted by GCC, but Clang rejects it with the error:
```
comparison of address of base class subobject 'A' of class 'B' to field 'a' has unspecified value
```
Demo: https://gcc.godbolt.org/z/9G8szToPz

It looks like Clang is right here, and this indeed must be rejected.
Comment 1 Drea Pinski 2021-12-04 13:32:01 UTC
Confirmed.
ICC also rejects this code with the following error message:
<source>(7): note: pointer values cannot be compared because they do not point into the same complete object or they point to subobjects with different accessibility


Note MSVC "accepts" it (if you change < to <= so the static_assert is ok).