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 15:27:54 +0000
I've implemented "std::is_nontrivially_relocatable" and
"std::relocate", and I've changed the implementation of
'std::unaligned' so that it works similar to how
'std::synchronised_value' works (i.e. you deal with an intermediary
proxy object on the stack).

So we start out with a variable:

    std::unaligned<T> obj;

And then if we want its value we can do:

    T monkey = obj.align().value();

Or alternatively we can do:

    std::unaligned<T> obj;
    auto manipulator = obj.align();
    T &t = manipulator.value();
    // Now we can do whatever we want with 't'

Here's my latest Godbolt (adapted from Connor Horman's original code):

    https://godbolt.org/z/boqvrj8nv

Until I patch Arthur's P1144 compiler to add a new method "void
std::string::_Relocate(void*) & noexcept", I am using the following
trick:

        unaligned<std::string> fish('\0',256u);
        fish.align().value().resize(0u);

to ensure that the string is relocatable (because the libstdc++
implementation of std::string contains a pointer to itself when the
string is below a certain size).

Arthur, do you have a link to a source code version control system
where I can patch your compiler?

Received on 2023-12-16 15:27:50