Tech News
← Back to articles

Is C++26 getting destructive move semantics?

read original related products more articles

Can I express a function that consumes an object? Meaning that its destructor is not run on the moved-from object?

Like the proposed library function trivially_locate_at itself?

template T* trivially_relocate_at(T* dst, T* src);

Naively, if the library authors can, so should I.

Problem: Where is the magic sauce? That function signature does not convey that it effectively destructs an object at src , or the reverse problem, that it effectively constructs an object at dst .

I suspect the answer is no: The few examples I have found are avoiding it by doing manual memory management with placement-new and std::destroy_at.

Reason for asking: I would like to propose what seems missing: Two new pointer qualifiers to express giving and taking ownership. If you can excuse my reuse of the new and delete keywords for a moment (it doesn't have to be those):

template T* trivially_relocate_at(new T* dst, delete T* src);

This is not about optimizing C++, but salvaging it: In order to have static lifetime analysis (akin to Rust) in C and/or C++, I see no way around adding an ability to express static ownership transfer.