Date: Wed, 29 Oct 2025 17:16:43 -0400
On Wed, Oct 29, 2025 at 5:05 PM Jason McKesson via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> On Wed, Oct 29, 2025 at 3:27 PM Sebastian Wittmeier via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >
> > sort was just an example.
> >
> >
> >
> > Should generic algorithms prefer swap, when possible?
>
> If it makes sense. The two aren't in competition. One exchanges two
> objects that already exist; the other translocates an object to a
> different piece of storage, creating a new object in the process.
> They're pretty different operations.
>
"Swap" and "relocate" are two different semantic operations, but it is true
that "swap" can be implemented *trivially* (as-if by swapping bytewise)
precisely when "relocate" can be implemented trivially (as-if by relocating
bytewise).
One of the killer apps for trivial relocation (although not for the P2786
version that got into C++26, sadly) is that when a type is trivially
relocatable, it is also trivially swappable and thus trivially rotate-able,
and (as Sean Parent once said) lots of things are rotates.
You can see this live in my libc++ fork:
https://godbolt.org/z/GWfhe6zrn
struct T {
std::shared_ptr<int> a_;
std::unique_ptr<int> b_;
};
#ifdef __cpp_lib_trivially_relocatable
static_assert(std::is_trivially_relocatable_v<T>);
#endif
void f(T *p, int n) {
std::rotate(p, p+1, p+n);
}
Notice the efficient codegen for `rotate` when `T` is understood to be
trivially relocatable.
–Arthur
std-proposals_at_[hidden]> wrote:
> On Wed, Oct 29, 2025 at 3:27 PM Sebastian Wittmeier via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >
> > sort was just an example.
> >
> >
> >
> > Should generic algorithms prefer swap, when possible?
>
> If it makes sense. The two aren't in competition. One exchanges two
> objects that already exist; the other translocates an object to a
> different piece of storage, creating a new object in the process.
> They're pretty different operations.
>
"Swap" and "relocate" are two different semantic operations, but it is true
that "swap" can be implemented *trivially* (as-if by swapping bytewise)
precisely when "relocate" can be implemented trivially (as-if by relocating
bytewise).
One of the killer apps for trivial relocation (although not for the P2786
version that got into C++26, sadly) is that when a type is trivially
relocatable, it is also trivially swappable and thus trivially rotate-able,
and (as Sean Parent once said) lots of things are rotates.
You can see this live in my libc++ fork:
https://godbolt.org/z/GWfhe6zrn
struct T {
std::shared_ptr<int> a_;
std::unique_ptr<int> b_;
};
#ifdef __cpp_lib_trivially_relocatable
static_assert(std::is_trivially_relocatable_v<T>);
#endif
void f(T *p, int n) {
std::rotate(p, p+1, p+n);
}
Notice the efficient codegen for `rotate` when `T` is understood to be
trivially relocatable.
–Arthur
Received on 2025-10-29 21:17:02
