gpt4 book ai didi

c++ - C++ 中的值语义和 move 语义之间有什么联系?

转载 作者:行者123 更新时间:2023-11-30 00:44:07 28 4
gpt4 key购买 nike

有很多文章讨论值语义与引用语义,也许更多的文章试图解释 move 语义。然而,从来没有人谈论过值语义和 move 语义之间的联系。它们是正交概念吗?

注意:这个问题不是关于比较值语义和 move 语义,因为很明显这两个概念不是“可比较的”。这个问题是关于它们是如何连接的,特别是(如@StoryTeller 所说),关于讨论(如何):

Move semantics help facilitate more use of value types.

最佳答案

来自original move proposal :

Copy vs Move

C and C++ are built on copy semantics. This is a Good Thing. Move semantics is not an attempt to supplant copy semantics, nor undermine it in any way. Rather this proposal seeks to augment copy semantics. A general user defined class might be both copyable and movable, one or the other, or neither.

The difference between a copy and a move is that a copy leaves the source unchanged. A move on the other hand leaves the source in a state defined differently for each type. The state of the source may be unchanged, or it may be radically different. The only requirement is that the object remain in a self consistent state (all internal invariants are still intact). From a client code point of view, choosing move instead of copy means that you don't care what happens to the state of the source.

For PODs, move and copy are identical operations (right down to the machine instruction level).

我想有人可以补充说:

move 语义允许我们保留值语义,但同时在原始(复制自)对象的值对程序逻辑不重要的情况下获得引用语义的性能。

关于c++ - C++ 中的值语义和 move 语义之间有什么联系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50198991/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com