C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Relocation in C++

From: Edward Catmur <ecatmur_at_[hidden]>
Date: Mon, 30 May 2022 16:15:22 -0600
On Mon, 30 May 2022 at 12:07, Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
wrote:

> On Mon, May 30, 2022 at 1:56 PM Edward Catmur <ecatmur_at_[hidden]>
> wrote:
>
>> [...]
>>
> template<class T> requires std::is_noexcept_relocatable_v<T>
>> void swap(T& lhs, T& rhs) {
>> T temp = relocate_at(&lhs);
>> new (&lhs) T(relocate_at(&rhs));
>> new (&rhs) T(relocate_at(&temp));
>> }
>>
>> (relocate_at is the magic library function that (destroys and) relocates
>> its argument into a returned prvalue.)
>>
>
> Terminology nit: `relocate_at` takes a pointer parameter `dest`, just like
> `construct_at` and `destroy_at`.
> *`relocate` with no suffix* is the magic library function that relocates
> into a returned prvalue.
> https://quuxplusone.github.io/blog/2022/05/18/std-relocate/
>

Ah, yeah, thanks. And that last relocate is incorrect; it should use the
relocation operator keyword, since temp should not be destroyed at the end
of scope:

template<class T> requires is_noexcept_relocatable_v<T>
void swap(T& lhs, T& rhs) {
    T temp = relocate(&lhs);
    relocate_at(&rhs, &lhs);
    new (&rhs) T(reloc temp);
}

Received on 2022-05-30 22:15:35