我是如何明白C++的move semantics?移动语义和perfect forwarding

其实主要就是三篇文章(博客):

首推这篇。

http://thbecker.net/articles/rvalue_references/section_01.html

从这里你可以知道什么时候你会知道,什么时候能够 “链式地” 调用移动构造函数而什么时候不能 ,明白其中过程(特别是什么时候不能)的一些细微差别,你就差不多懂什么是move semantic了。

然后是scott meyers的 Universal Reference In C++11 :

https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers

当作是第一篇的补充说明吧。如果你还有些疑惑,或者将信将疑,那么这篇可以让你印证你的想法。

然后是2002年的一篇关于move semantic的C++提案:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm

从这里你可以知道,原来move semantics只对那些大的对象才有意义,对于内置类型(如int),move和copy没有区别的("down to the machine instructions")。(知道这个对我来说真的很重要,因为知道在最底层,对于内置类型,如int,move和copy是没有啥区别的,多多少少打消了std::move()的神秘色彩)

第一篇和第三篇都有讲到perfect forwarding。衍生物嘛。

也可以看看scott meyers在2013年的一个演讲,关于universal reference的,讲得很好(scott meyers 这是天生段子手啊):

https://channel9.msdn.com/events/GoingNative/2013/An-Effective-Cpp11-14-Sampler(应该是这个了吧...网速太慢打不开来看)

最后,当然可以看看stackoverflow上面的东西:

http://stackoverflow.com/questions/3106110/what-are-move-semantics

(从这里你不仅可以了解move sematics,还可以知道一下什么事copy and swap technique(rule of three).

:)