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

前端 未结 5 1754
予麋鹿
予麋鹿 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.

提交回复
热议问题