- Not all class objects have a moved-from state

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

 - Some objects can't even store a flag; to add id nevertheless, why not use std::optional?

 

your 3 changes work with std::optional<T>:

1) destruction of a known-to-be-empty std::optional will be optimized to a no-op

2) getting an empty std::optional is possible

3) creating/emplacing an empty std::optional in raw memory is possible

 

What you probably want instead is an in-band signalled optional interface. There sure are some C++ papers for it? Like a HANDLE; and 0, or -1, or nullptr signals empty. And the zero HANDLE can just be destructed as a NO-OP whereas the valid HANDLE needs deallocation or closing some hardware interface.


 

-----Ursprüngliche Nachricht-----
Von: Frederick Virchanza Gotham via Std-Proposals <std-proposals@lists.isocpp.org>
Gesendet: Mo 27.10.2025 11:48
Betreff: Re: [std-proposals] Replace an object -- but retain old object if new object fails to construct
An: std-proposals@lists.isocpp.org;
CC: Frederick Virchanza Gotham <cauldwell.thomas@gmail.com>;
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)) );
       . . .
   }
--
Std-Proposals mailing list
Std-Proposals@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals