C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Wed, 20 Dec 2023 17:22:52 +0000
On Wed, Dec 20, 2023 at 1:39 PM Frederick Virchanza Gotham wrote:
>
> Tested and working: https://godbolt.org/z/qK736evY7


Here's my latest implementation of 'std::unaligned' that has a
specialisation of the '_Relocate' function which should work fine with
libstdc++'s nonrelocatable implementation of 'basic_string'. The test
code works fine with 'std::string' and also 'std::basic_string<long
double>':

        https://godbolt.org/z/bxWjssocr

By the way I think the standard library should have a function to test
the alignment of a pointer:

    template<typename T>
    inline std::ptrdiff_t alignment_modulus(void const *const arg) noexcept
    {
        assert( nullptr != arg );
    #ifdef UINTPTR_MAX // If this compiler has std::uintptr_t
        return reinterpret_cast<std::uintptr_t>(arg) % alignof(T);
    #else
        // This implementation does not need std::uintptr_t (which
might not exist)
        using std::byte;
        std::ptrdiff_t retval = alignof(T);
        byte *p = static_cast<byte*>(const_cast<void*>(arg));
        std::size_t buffer_size = sizeof(T);
        for ( ; ; ++p, --retval )
        {
            if ( nullptr != std::align(alignof(T), sizeof(T),
*static_cast<void**>(static_cast<void*>(p)), buffer_size) )
            {
                return retval % alignof(T);
            }
        }
    #endif
    }

Received on 2023-12-20 17:22:44