C++ Logo

std-proposals

Advanced search

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

From: connor horman <chorman64_at_[hidden]>
Date: Sun, 10 Dec 2023 20:27:54 -0500
As a note about `std::unaligned`, it's possible to implement `unaligned` in
C++20 in fully standards compliant code.
https://godbolt.org/z/MsWhsjoW3

It has a converting constructor and conversion operator to and from a
trivially copyable `T`. It has each special member function (including
default constructor) trivial iff `T`s corresponding special member function
is trivial, and its deleted otherwise (notably, `T` can have a non-trivial
default constructor and the type will otherwise have all operators).

The only issue is that it will not match the ABI of `T` or a
`__attribute__((packed)` struct (which is passed in memory on Sys-V x86_64
if any field of `T` has alignment > 1).

This can also be modified to match the expanded `#[repr(packed)]` attribute
from the Rust language, which allows you to specify a maximum alignment,
which can be any value, not just 1: https://godbolt.org/z/rPWTPe8G1

On Sun, 10 Dec 2023 at 11:59, Thiago Macieira via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Sunday, 10 December 2023 08:22:23 PST Frederick Virchanza Gotham via
> Std-
> Proposals wrote:
> > Lénárd posted the same day:
> > > And lift the ban on specifying
> > > lower than the types alignment
> > > for members.
> >
> > Actually it would be interesting if we could do the following:
> >
> > struct Monkey {
> > alignas(1) long unsigned a;
> > alignas(1) char b;
> > alignas(1) long unsigned c;
> > alignas(1) char d;
> > };
>
> But it would also allow applying the attribute to arbitrary types, which
> it
> shouldn't. The text would need to restrict it to only primitives (or at
> least
> trivial ones) and deny calling members on them.
>
> It's far easier to accomplish the necessary restrictions with a library
> solution.
>
> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Software Architect - Intel DCAI Cloud Engineering
>
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-12-11 01:28:08