C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 28 Jan 2025 09:42:20 +0000
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.

Received on 2025-01-28 09:42:26