C++ Logo

std-proposals

Advanced search

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

From: Tiago Freire <tmiguelf_at_[hidden]>
Date: Mon, 27 Jan 2025 12:20:34 +0000
> I want C++26 to explicitly state: "All pointer types are intercompatible -- they all have the same size and representation", which would allow code like:
> char c;
> long double *p = &c;
> char *p2 = p;
> *p2 = 'a';
> The main use for this would be in allowing data to be misaligned in memory. For example let's consider "std::basic_string<char32_t>" which contains a pointer into its own buffer. If such an object is misaligned in memory, then the pointer into the buffer might not accurately point at the buffer. But I want the Standard to be changed so that all pointers are the same.
>
> There were rumours years and years ago of systems that had different size pointers but they're all extinct now.

That's kind of a completely different ask than "being intercompatible" because of "systems that had different size pointers".

What you are asking is not so much about the pointer itself, but about the data that the pointer is pointing too, and either or not it should be miss-aligned.

And the answer is no! Absolutely not.

Is a "double pointer" that pointing to something, pointing at a double?
The answer is, it should!
And if the underlying data type has alignment requirements then it stands to reason that the compiler should be allowed to make assumptions regarding the alignment of that data being pointed too.

If type A does not necessarily align to type B, then why are you advertising a pointer to A to be a pointer to B?
This should be considered an invalid type alias.
What you are trying to do might theoretically work, but from a structured programming language perspective maybe it shouldn't!
And the standard shouldn't cater to obvious bad design.

Received on 2025-01-27 12:20:37