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: Mon, 27 Oct 2025 10:48:59 +0000
On Fri, Oct 24, 2025 at 4:08 PM Thiago Macieira wrote:
>
> std::optional already guarantees that.


My original use case used 'optional', but then Arthur came in and made
it more generic with a function as follows:

    template<typename T, typename... Params>
    void replace(T*, Params&&...);

and so sometimes we might want to be certain that the object is safe to destroy.

I'm going off on a bit of a tangent here, but with regard to objects
that have been moved from, I wonder would the following three changes
be welcome in the language?

Change No. 1: The destruction of a moved-from object should always
be equivalent to a no-op and can therefore be elided
Change No. 2: A new consteval standard library function that
returns a moved-from PRvalue:

    template<typename T>
    requires (!is_reference_v<T>)
    consteval T moved_from(void)
    {
        . . .
    }

Change No. 3: A new constexpr standard library function that
emplaces a moved-from object:

    template<typename T>
    requires (!is_reference_v<T>)
    void emplace_moved_from(void *const p)
    {
        assert( 0 == (reinterpret_cast<uintptr>(p) % alignof(T)) );
        . . .
    }

Received on 2025-10-27 10:48:34