C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Replace an object -- but retain old object if new object fails to construct

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Wed, 29 Oct 2025 14:16:11 +0000
On Wed, Oct 29, 2025 at 2:05 PM Sebastian Wittmeier wrote:
>
> But then the question is, how does trivial relocatibility interact
> with memcpy and other ways to access the raw byte representation
> of the object.


I really hope nobody suggests that 'memcpy' or 'memmove' shall have
special behaviour when the object being relocated is polymorphic . . .
. I really hope nobody goes there.

. . . and even if someone does go there, then what about a good
old-fashioned byte-by-byte loop such as the following code?

    #include <cstddef>
    #include <type_traits>

    template<typename T>
    requires std::is_trivially_relocatable_v<T>
    void Relocate(T const *const p, T *const q)
    {
        char unsigned const *from = (char unsigned const *)p;
        char unsigned *to = (char unsigned *)q;
        for ( std::size_t i = 0u; i < sizeof(T); ++i ) *to++ = *from++;
    }

    int main(void)
    {
        int a = 2, b = 3;
        Relocate(&a, &b);
    }

By the way I'm not sure whether to use 'sizeof' or '__datasizeof' just
in case of [[no_unique_address]].

Received on 2025-10-29 14:16:23