Date: Tue, 28 Jan 2025 10:55:55 +0100
sizeof(char*) could be different
Is the cast of pointers allowed for unrelated types? I think it is UB in many cases.
The representation of pointers for different types could be different. I think it is possible to create an implementation that stores the dynamic or static type within the e.g. MSBs of the pointer.
Those rules can be changed in the standard, but would it increase performance or safety or worsen it?
-----Ursprüngliche Nachricht-----
Von:Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]>
Gesendet:Di 28.01.2025 10:42
Betreff:Re: [std-proposals] Make all data pointers intercompatible
An:std-proposals_at_[hidden];
CC:Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>;
Not entirely sure where this thread has ventured off to, but let me
get back to what I was talking about (i.e. the size of pointers).
Given the following translation unit:
class MyClass;
MyClass *my_global_variable;
The compiler doesn't know the alignment requirements of MyClass. For
instance it might be:
struct MyClass { char c; };
or it could be:
struct MyClass { long double f; };
Therefore I think we can make the following assertion:
sizeof(void*) == sizeof(char*) == sizeof( any class pointer )
This means that we should be able to do the following:
class SomeClass;
void *GiveMeBackTheArgumentUnchanged(void *const arg)
{
SomeClass *const p = static_cast<SomeClass*>(arg);
return p;
}
The above code should work fine on every C++ compiler because a
SomeClass* should be the same as a void*.
The only complication though is that the following pointers could
still be smaller than a void*:
short*
int*
long*
long long*
float*
double*
long double*
But since there's no compiler alive today that actually does that, I
think C++26 should mandate that all data pointers are the same size
and representation.
--
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2025-01-28 09:59:29