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

前端 未结 5 1735
予麋鹿
予麋鹿 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:11

    D have separate value and object semantics :

    • if you declare your type as struct, it will have value semantic by default
    • if you declare your type as class, it will have object semantic.

    Now, assuming you don't manage the memory yourself, as it's the default case in D - using a garbage collector - you have to understand that object of types declared as class are automatically pointers (or "reference" if you prefer) to the real object, not the real object itself.

    So, when passing vectors around in D, what you pass is the reference/pointer. Automatically. No copy involved (other than the copy of the reference).

    That's why D, C#, Java and other language don't "need" moving semantic (as most types are object semantic and are manipulated by reference, not by copy).

    Maybe they could implement it, I'm not sure. But would they really get performance boost as in C++? By nature, it don't seem likely.

    0 讨论(0)
  • 2020-12-15 04:11

    I think if you need the source to loose the resource you might be in trouble. However being GC'ed you can often avoid needing to worry about multiple owners so it might not be an issue for most cases.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-15 04:15

    I think all answers completely failed to answer the original question.

    First, as stated above, the question is only relevant for structs. Classes have no meaningful move. Also stated above, for structs, a certain amount of move will happen automatically by the compiler under certain conditions.

    If you wish to get control over the move operations, here's what you have to do. You can disable copying by annotating this(this) with @disable. Next, you can override C++'s constructor(constructor &&that) by defining this(Struct that). Likewise, you can override the assign with opAssign(Struct that). In both cases, you need to make sure that you destroy the values of that.

    For assignment, since you also need to destroy the old value of this, the simplest way is to swap them. An implementation of C++'s unique_ptr would, therefore, look something like this:

    struct UniquePtr(T) {
        private T* ptr = null;
    
        @disable this(this); // This disables both copy construction and opAssign
    
        // The obvious constructor, destructor and accessor
        this(T* ptr) {
            if(ptr !is null)
                this.ptr = ptr;
        }
    
        ~this() {
            freeMemory(ptr);
        }
    
        inout(T)* get() inout {
            return ptr;
        }
    
        // Move operations
        this(UniquePtr!T that) {
            this.ptr = that.ptr;
            that.ptr = null;
        }
    
        ref UniquePtr!T opAssign(UniquePtr!T that) { // Notice no "ref" on "that"
            swap(this.ptr, that.ptr); // We change it anyways, because it's a temporary
            return this;
        }
    }
    

    Edit: Notice I did not define opAssign(ref UniquePtr!T that). That is the copy assignment operator, and if you try to define it, the compiler will error out because you declared, in the @disable line, that you have no such thing.

    0 讨论(0)
  • 2020-12-15 04:17

    I believe that there are several places in D (such as returning structs) that D manages to make them moves whereas C++ would make them a copy. IIRC, the compiler will do a move rather than a copy in any case where it can determine that a copy isn't needed, so struct copying is going to happen less in D than in C++. And of course, since classes are references, they don't have the problem at all.

    But regardless, copy construction already works differently in D than in C++. Generally, instead of declaring a copy constructor, you declare a postblit constructor: this(this). It does a full memcpy before this(this) is called, and you only make whatever changes are necessary to ensure that the new struct is separate from the original (such as doing a deep copy of member variables where needed), as opposed to creating an entirely new constructor that must copy everything. So, the general approach is already a bit different from C++. It's also generally agreed upon that structs should not have expensive postblit constructors - copying structs should be cheap - so it's less of an issue than it would be in C++. Objects which would be expensive to copy are generally either classes or structs with reference or COW semantics.

    Containers are generally reference types (in Phobos, they're structs rather than classes, since they don't need polymorphism, but copying them does not copy their contents, so they're still reference types), so copying them around is not expensive like it would be in C++.

    There may very well be cases in D where it could use something similar to a move constructor, but in general, D has been designed in such a way as to reduce the problems that C++ has with copying objects around, so it's nowhere near the problem that it is in C++.

    0 讨论(0)
提交回复
热议问题