求大神关于移动语义的问题
若要研究,用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.
推荐阅读
- 过节■江苏省委省政府办公厅下发关于做好2021年元旦春节期间有关工作的通知
- 西藏首台5G网络车载移动CT仪器投用
- 教育|中国移动“和教育”智慧校园研讨会在通召开
- 夕阳红|南通移动:“智慧养老”手机让“夕阳红”不孤单
- |徐州市出台《关于优化创新创业生态系统 提升区域科技创新活力的实施意见》及实施细则
- 怎样评价华为、诺基亚、中兴中标中国移动高端路由交换设备扩容集采
- 雨下|全球关于禁售燃油车只是理论上可行吗
- 关于用phpfsocket 写Post, 模拟http 报文怎样写入要传输的处理数据
- 为啥Smartisan OS只能支持移动的VoLTE
- 智叔|很多家长还在整箱买:谈谈关于牛奶的17个真相警惕这些列入黑名单的“假牛奶”