C++ Logo

std-proposals

Advanced search

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

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Sun, 17 Dec 2023 11:17:06 -0500
On Sun, Dec 17, 2023 at 8:11 AM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:

> On Sat, Dec 16, 2023 at 6:48 PM Frederick Virchanza Gotham wrote:
> >
> > I've further modified it to be more convenient when T is a pointer:
>
> Arthur, in order to patch your P1144 compiler to give 'std::string' a
> new method called '_Relocate', should I edit the following file?
>
>
> https://github.com/Quuxplusone/gcc/blob/trivially-relocatable/libstdc%2B%2B-v3/include/bits/basic_string.h
>
> Or do you have it somewhere else?


Yes and no. My Clang/libc++ fork lives at
https://github.com/Quuxplusone/llvm-project/tree/trivially-relocatable
so if you want to change the std::string it *normally* uses on Godbolt,
you'd want to mess with this file:
https://github.com/Quuxplusone/llvm-project/blob/trivially-relocatable/libcxx/include/string
But libc++'s std::string is already *trivially* relocatable. So in fact
you're correct that in order to modify the non-trivially-relocatable
*libstdc++* std::string (which is also available on Godbolt since about a
week ago when you pass -stdlib=libstdc++), you'd modify the libstdc++ file
you listed above.

However, I'm unconvinced thus far that your "_Relocate by memcpy plus some
fixup" really provides significant benefit over the status quo of "just
call the move-constructor and then destroy the RHS" (which is what P1144
`std::relocate_at` already does for types that aren't trivially
relocatable).
Here's a hacked-up example: https://godbolt.org/z/K1MaPx34K
Now, the `-DX` version looks vastly better. But note that:
(1) the libstdc++ maintainer could have achieved the same thing by
overloading `std::relocate_at` for strings (and in fact I might do that,
eventually);
(2) the benefit *does not* propagate to user-defined types such as `struct
S { std::string m_; }`.
Propagating the benefit automatically for Rule-of-Zero types is the main
point of P1144 (for trivially relocatable types) and P2785
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2785r3.html> (for
relocatable types in general).

HTH,
Arthur

Received on 2023-12-17 16:17:20