Are C++11 move semantics doing something new, or just making semantics clearer?

后端 未结 6 1082
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 04:29

I am basically trying to figure out, is the whole \"move semantics\" concept something brand new, or it is just making existing code simpler to implement? I am always interested

6条回答
  •  心在旅途
    2021-02-01 04:58

    There is absolutely something new going on here. Consider unique_ptr which can be moved, but not copied because it uniquely holds ownership of a resource. That ownership can then be transferred by moving it to a new unique_ptr if needed, but copying it would be impossible (as you would then have two references to the owned object).

    While many uses of moving may have positive performance implications, the movable-but-not-copyable types are a much bigger functional improvement to the language.

    In short, use the new techniques where it indicates the meaning of how your class should be used, or where (significant) performance concerns can be alleviated by movement rather than copy-and-destroy.

提交回复
热议问题