求大神关于移动语义的问题

若要研究,用GCC / Clang加上-fno-elide-constructors 选项,避免编译器进行copy constructor elison,这样就会出现move constructor的调用
■网友
关于右值和移动语义看两篇文章:Thomas Becker:http://thbecker.net/articles/rvalue_references/section_01.htmlScott Meyer:https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers
■网友
返回值优化么?直接return 的话,编译器会返回值优化,把这个值直接构造在返回值的地方。name n return n的话,编译器会先构造n,然后结束的时候 在返回值的地方复制一个n。这里会用移动拷贝。
■网友
这两种情况,编译器都有权干掉你的移动构造函数(不管你在里面干了什么),我试了下vs2015(clang)和mingw-w64在O3下都是一致的结果,没有移动构造。见下(摘自n4140,n4141即c++14,ps:c++11里这部分是一样的),n1+n2满足第一种情况,用n1+n2的结果构造n4满足第三种情况。12.8.31. When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization. This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):— (31.1) in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unquali?ed type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value— (31.2) in a throw-expression, when the operand is the name of a non-volatile automatic object (other than a function or catch-clause parameter) whose scope does not extend beyond the end of the innermost enclosing try-block (if there is one), the copy/move operation from the operand to the exception object (15.1) can be omitted by constructing the automatic object directly into the exception object— (31.3) when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unquali?ed type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move— (31.4) when the exception-declaration of an exception handler (Clause 15) declares an object of the same type (except for cv-quali?cation) as the exception object (15.1), the copy operation can be omitted by treating the exception-declaration as an alias for the exception object if the meaning of the program will be unchanged except for the execution of constructors and destructors for the object declared by the exception-declaration.


    推荐阅读