C++ Logo


Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Fri, 15 Dec 2023 14:15:04 +0000
On Thursday, December 14, 2023, Thiago Macieira wrote:

> And it took me 4.2 seconds to find a counter-example proving that it can't
> be
> done generally for non-trivial types. One counter-example suffices to
> disprove,
> leading me to post that the idea was DoA.
> It might be possible for non-trivial but trivially-relocatable types using
> your technique. But that's not what you proposed.

You only showed that 'unaligned' won't work for types that are not
trivially relocatable, using one compiler's implementation of 'std::string'
as an example (the one where 'std::string' contains a pointer into itself).

That implementation of std::string could be given a new member function
something like:

void string::_Relocate(void *const p) noexcept
    if ( (this->ptr >= this->buf) && (this->ptr < (this-> buf +
sizeof(this->buf)) )
        this->ptr += (char*)p - (char*)this;

Then we could write a constraint that detects the member function
'_Relocate' and call it 'std::is_nontrivially_relocatable', and so then the
template class 'std::unaligned' could have the following constraint:

    requires is_trivially_relocatable_v<T> ||

I'm on holidays in the Canaries right now typing this up on my phone and
want to limit my screen time while I'm here, but soon I'll revise my
Godbolt to work with any type that can be relocated. But I think I'll have
to get rid of 'constexpr operator T()' because then T would need to be
copy-constructible. (Actually I might leave it in but have a 'requires'
that T be std::is_copy_constructible<T>).

I'm telling you, we can write a very versatile 'std::unaligned' that will
work with any type that is "is_trivially_relocatable_v<T> ||

Received on 2023-12-15 14:15:07