C++ Logo


Advanced search

Re: [std-proposals] [[packed]] std::unaligned

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 19 Dec 2023 17:07:40 +0000
Replying to Thiago and Arthur in series below . . .

On Mon, Dec 18, 2023 at 9:59 PM Thiago Macieira wrote:
> We might as well not have private and protected in the language then. Why
> don't you write a paper to suggest we deprecate them?

'private' and 'protected' are useful, in particular when it comes to SFINAE.

But even though I have a lock on the front door of my house, I have a
key for it hidden nearby. It's not bad practise to provide a way to
circumvent a system in unusual circumstances. I mean people are
already editing proprietary header files to change members from
private to public, or even worse: "#define private public". If they
can have safe spaces for injections in big cities to minimise harm
then we can also have a way of forcing friendship in C++. Better to do
it safely and without violating SFINAE then going all out with
"#define private public".

On Mon, Dec 18, 2023 at 11:25 PM Arthur O'Dwyer wrote:
> But you cannot have the mental model that "an object of type T" ever exists in
> misaligned space, or that you're "relocating" from an object of type T into
> misaligned space. That's simply physically impossible.

The whole 'object model' is already a bit metaphysical and airy-fairy
and make-believe. I have to really force myself to take functions like
"start_lifetime_as" seriously (maybe it's because I program
microcontrollers all day and am a bit closer to the copper).

Just as an aside. . . if an object contains a pointer to itself . . .
then relocating it from unaligned space into aligned space could get a
little more complicated if the pointer inside it is anything other
than 'void*' or 'char*', because a pointer such as 'long double*'
might have less bits of precision for the memory address than a
'char*'. (By the way has there ever been such an implementation of
C++?). I mean when you do:

    char c;
    char *p = &c;
    long double *p2 = static_cast<long double*>(static_cast<void*>(p));

and then convert it back:

    char *p3 = static_cast<char*>(static_cast<void*>(p2));

then you might not get back the original memory address. Although
thinking about it now, this might not be a problem, as the least
significant bits will be zeroed-out when you eventually realign it
again (and you want them zeroed out).

Received on 2023-12-19 17:07:32