C++ Logo

std-proposals

Advanced search

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

From: Julien Villemure-Fréchette <julien.villemure_at_[hidden]>
Date: Thu, 21 Dec 2023 17:48:56 -0500
FWI, std::basic_string<long
double> is not a well chosen example as 'long double' is not a character container type: std::char_traits<long double> need not be defined by an implementation (and should probably not be). The primary template for std::char_traits could as well be left undefined.

At least, it would be more appealing to use a more sensible type as a toy example, say std::u32string (AKA std::basic_string<char32_t>).

Julien V.


On December 20, 2023 12:22:52 p.m. EST, Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> wrote:
>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
> }
>--
>Std-Proposals mailing list
>Std-Proposals_at_[hidden]
>https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-12-21 22:49:04