C++26 是否会获得析构移动语义?
Is C++26 getting destructive move semantics?

原始链接: https://stackoverflow.com/questions/79817124/is-c26-getting-destructive-move-semantics

作者探讨了在C++函数中表达所有权转移的困难,特别是当函数“消耗”一个对象但不运行其原始析构函数时。现有的解决方案,如`trivially_relocate_at`,缺乏清晰地表明这种行为的信号,依赖于对内存操作的隐式理解。 核心问题是缺乏一种机制来静态地传达所有权变更——这对于安全性和分析至关重要,类似于Rust的所有权系统。作者建议使用新的指针限定符,暂定使用`new`和`delete`(但并非一定使用这些名称),来显式地标记指针是在函数调用期间被给予或获取的。 这并非关于性能,而是关于从根本上提高C++进行静态生命周期分析的能力,并通过清晰的所有权声明来防止内存安全问题。他们认为,这样的系统对于在C++中实现类似Rust的保证至关重要。

## C++26 与破坏性移动语义 - Hacker News 总结 最近 Hacker News 上的一场讨论集中在从 C++26 标准中移除破坏性移动语义这一特性。该特性旨在提高效率,但由于无法及时解决的一个关键错误而被放弃。这会影响与 Rust 等语言的互操作性潜在改进。 对话演变成关于 C++ 复杂性及其持续相关性的更广泛争论。一些人认为 C++ 仍然至关重要,因为它具有硬件兼容性和性能,尤其是在游戏开发、科学计算和人工智能等专业领域。另一些人则认为,与 Rust 等较新的语言相比,它不必要地复杂,并提到了别名规则、SFINAE 和复杂的内存模型等问题。 许多评论员强调了在 C++ 中平衡向后兼容性和现代化的挑战,并质疑该语言的设计选择是否给开发者带来了不必要的负担。 讨论还涉及 Rust 中的替代 GUI 框架以及强大的生态系统对语言采用的重要性。
相关文章

原文

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 <class T>
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 <class T>
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.

联系我们 contact @ memedata.com