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 11:41:40 +0000
On Mon, Oct 27, 2025 at 11:05 AM Sebastian Wittmeier wrote:
>
> - Not all class objects have a moved-from state


This can be determined from std::is_move_constructible_v.


> - Some objects need tidying up even in their moved-from state,
> e.g. freeing memory from the heap


I think C++11 was too liberal with what a 'moved-from' object can do .
. . I would have limited it to "safe to destroy". I would _not_ have
made it safe to assign, nor safe to invoke methods on.

If you really want to assign to a moved_from object, I think just do:

    Func( std::move(var) );
    ::new( &var ) T( SomeOtherFunc() );

Received on 2025-10-27 11:41:14