C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sat, 16 Dec 2023 18:48:32 +0000
On Sat, Dec 16, 2023 at 6:01 PM Frederick Virchanza Gotham wrote:
>
> On Sat, Dec 16, 2023 at 3:27 PM Frederick Virchanza Gotham wrote:
> >
> > unaligned<std::string> fish('\0',256u);
> > fish.align().value().resize(0u);
>
>
> I've further modified the code so that that second line can be simplified to:
>
> fish->resize(0u);
>
> Here's the GodBolt: https://godbolt.org/z/1hcPnxn3s


I've further modified it to be more convenient when T is a pointer:

        auto operator->(void)
        {
            if constexpr ( std::is_pointer_v<T> ) return
static_cast<T&>(*static_cast<T*>(static_cast<void*>(buf)));
            else /******************************/ return
static_cast<T*>( static_cast<void*>(buf) );
        }

which means you can do this:

    unaligned<string*> monkey(&some_other_string);
    monkey->resize(10u);

Also I corrected the implementations of the assignment operators that
were missing 'return *this;' at the end.

GodBolt: https://godbolt.org/z/ab6T6T613

Received on 2023-12-16 18:48:28