Does D have something akin to C++0x's move semantics?

前端 未结 5 1748
予麋鹿
予麋鹿 2020-12-15 03:45

A problem of \"value types\" with external resources (like std::vector or std::string) is that copying them tends to be quite expensive, a

5条回答
  •  抹茶落季
    2020-12-15 04:14

    I somehow have the feeling that actually the rvalue references and the whole concept of "move semantics" is a consequence that it's normal in C++ to create local, "temporary" stack objects. In D and most GC languages, it's most common to have objects on the heap, and then there's no overhead with having a temporary object copied (or moved) several times when returning it through a call stack - so there's no need for a mechanism to avoid that overhead too.

    In D (and most GC languages) a class object is never copied implicitly and you're only passing the reference around most of the time, so this may mean that you don't need any rvalue references for them.

    OTOH, struct objects are NOT supposed to be "handles to resources", but simple value types behaving similar to builtin types - so again, no reason for any move semantics here, IMHO.

    This would yield a conclusion - D doesn't have rvalue refs because it doesn't need them.

    However, I haven't used rvalue references in practice, I've only had a read on them, so I might have skipped some actual use cases of this feature. Please treat this post as a bunch of thoughts on the matter which hopefully would be helpful for you, not as a reliable judgement.

提交回复
热议问题