C++ Logo

std-proposals

Advanced search

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

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Tue, 12 Dec 2023 08:48:45 -0500
On Tue, Dec 12, 2023 at 7:24 AM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:

>
> Actually change that to:
>
> ~unaligned(void) {
> if constexpr ( !std::is_trivially_destructible<T> ) {
> alignas(T) std::byte buf[ __datasizeof(T) ];
> T &tmp = *static_cast<T*>(static_cast<void*>(buf));
> std::memcpy(&tmp, &_m_data.front(), __datasizeof(T));
> tmp.~T();
> }
> }
>
> And then add wording to the standard something like:
> "An implementation of a destructor shall be considered trivial if,
> after expanding an 'if constexpr', the body of the destructor is empty"
>

No, C++ never cracks open a curly-braced body to "inspect" it like that
(because, in general, the Halting Problem).
Fortunately for you, C++20 already supports conditionally trivial special
member functions — you've even used that feature in your Godbolt already!

    ~unaligned() requires std::is_trivially_destructible<T> = default;
    ~unaligned() { alignas(T) std::byte ~~~~ tmp.~T(); }

The constrained destructor will be preferred by overload resolution when
[that is, for types for which] its constraint is satisfied.
The key term for which to Ctrl+F the standard is "*eligible* destructor."

HTH,
Arthur

Received on 2023-12-12 13:49:00