C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 12 Dec 2023 12:24:35 +0000
On Tuesday, December 12, 2023, Frederick Virchanza Gotham wrote:

> Therefore the destructor of 'unaligned' would have to do something like:
>
> ~unaligned(void)
> {
> 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();
> }
>


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"

And so then an 'aligned<T>' would be trivially destructible if a T is
trivially destructible.

Received on 2023-12-12 12:24:37