C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Idea: moved_from<T>() for Efficient Moved-From State Construction

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Thu, 24 Apr 2025 07:24:33 +0200
Hi Elazar, that proposal is similar to and could perhaps be merged with the active proposal P3357.   -----Ursprüngliche Nachricht----- Von:Elazar via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Do 24.04.2025 03:09 Betreff:[std-proposals] Idea: moved_from<T>() for Efficient Moved-From State Construction An:std-proposals_at_[hidden]; CC:Elazar <elazarg_at_[hidden]>; Hey everyone, I've been working on a new proposal for the C++ Standard Library that I'd like to share: "A Library-Based Mechanism for Moved-From Initialization (moved_from())" Problem: Creating an object just to immediately overwrite it, like:     BigExpensiveType obj;     // Default construction we don't need     obj = get_actual_value(); // Immediate overwrite This is especially wasteful for types with non-trivial constructors. Current workarounds like placement new or custom factory functions are either unsafe or non-standard. Proposal: Add a standardized moved_from<T>() function that constructs objects directly in a moved-from state. Type authors opt-in by implementing T::moved_from().     BigExpensiveType obj = moved_from<BigExpensiveType>();     obj = get_actual_value(); // Now we're just overwriting a moved-from state This shifts implementation responsibility from client code to the type author (who knows the most about safely initializing their type), while providing stronger guarantees for compiler optimizations. The full proposal is available here: https://gist.github.com/elazarg/ffe096f8e5c237d60a5d6f5205f50e2e What do you think? Are there specific use cases you'd like to see addressed? Any implementation concerns?   Looking forward to your thoughts! Best regards, Elazar Gershuni -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-04-24 05:31:11