C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Make all data pointers intercompatible

From: Bo Persson <bo_at_[hidden]>
Date: Mon, 27 Jan 2025 00:53:46 +0100
On 2025-01-26 at 22:25, Frederick Virchanza Gotham via Std-Proposals wrote:
> Eight months ago, I posted here saying that all pointer types should
> be made intercompatible. Here's my original post:
>
> https://lists.isocpp.org/std-proposals/2024/05/9773.php
>
> With all the talk today on the topic of "Public inheritance with
> banned conversion to base", I've just realised something. Consider the
> following two classes:
>
> struct Frog {
> char c;
> };
>
> struct Monkey {
> long long n;
> };
>
> Frog has an alignment of 1, whereas Monkey has an alignment of 8. So a
> pointer to a Frog could possibly need more bits than a pointer to a
> Monkey.
>
> But now let's consider another translation unit in the same program:
>
> struct Frog;
> struct Monkey;
>
> int main(void)
> {
> Frog *p = nullptr;
> Monkey *p2 = nullptr;
> }
>
> This second translation unit doesn't know the alignment requirements
> of either Frog or Monkey, but yet it can create a Frog* or a Monkey*.
> This means that one of the following must be true:
>
> (1) Any given class type will have its alignment requirements made
> more restrictive so that any "basic pointer" can point to it.
>
> (2) All pointers type are intercompatible and the same size.

Or (3) All pointers to class type are the same size.

Pointers to non-class types, like the earlier example of char* can still
have a different size.

Received on 2025-01-26 23:53:57