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

后端 未结 6 1084
佛祖请我去吃肉
佛祖请我去吃肉 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:42

    Since move semantics only apply in the presence of rvalue references, which are declared by a new token, &&, it seems very clear that they are something new.

    In principle, they are purely an optimizing techique, which means that: 1. you don't use them until the profiler says it is necessary, and 2. in theory, optimizing is the compiler's job, and move semantics aren't any more necessary than register.

    Concerning 1, we may, in time, end up with an ubiquitous heuristic as to how to use them: after all, passing an argument by const reference, rather than by value, is also an optimization, but the ubiquitous convention is to pass class types by const reference, and all other types by value.

    Concerning 2, compilers just aren't there yet. At least, the usual ones. The basic principles which could be used to make move semantics irrelevant are (well?) known, but to date, they tend to result in unacceptable compile times for real programs.

    As a result: if you're writing a low level library, you'll probably want to consider move semantics from the start. Otherwise, they're just extra complication, and should be ignored, until the profiler says otherwise.

提交回复
热议问题