copy elison & RVO & NRVO
To summarize, RVO is a compiler optimization technique, while std::move is just an rvalue cast, which also instructs the compiler that it's eligible to move the object. The price of moving is lower than copying but higher than RVO, so never apply std::move to local objects if they would otherwise be eligible for the RVO.
因此,在能够使用copy elision时,我们不要在return时加std::move()。在copy elision不work时,我们还是要加上std::move()从而调用move constructor而不是调用copy constructor.
博文中比较费解的是最后一个示例,这么写似乎产生了bug。
个人理解:最后一个示例触发了RVO,导致第一次拷贝:栈内变量拷贝至返回值临时变量被省略(两次拷贝参见下文),第二次拷贝:临时变量拷贝至具名变量未省略。而因为变量类型是右值引用,第二次的拷贝变成了移动构造。于是就出现了奇怪的输出。
Copy elision
In general, the C++ standard allows a compiler to perform any optimization, provided the resulting executable exhibits the same observable behaviour as if (i.e. pretending) all the requirements of the standard have been fulfilled. This is commonly referred to as the "as-if rule".[8] The term return value optimization refers to a special clause in the C++ standard that goes even further than the "as-if" rule: an implementation may omit a copy operation resulting from a return statement, even if the copy constructor has side effects.[1]
The following example demonstrates a scenario where the implementation may eliminate one or both of the copies being made, even if the copy constructor has a visible side effect (printing text).[1] The first copy that may be eliminated is the one where a nameless temporary C could be copied into the function f's return value. The second copy that may be eliminated is the copy of the temporary object returned by f to obj.
#include <iostream>
struct C {
C() {}
C(const C&) { std::cout << "A copy was made.\n"; }
};
C f() {
return C();
}
int main() {
std::cout << "Hello World!\n";
C obj = f();
return ;
}
Depending upon the compiler, and that compiler's settings, the resulting program may display any of the following outputs:
Hello World!
A copy was made.
A copy was made.
Hello World!
A copy was made.
Hello World!
这里有一篇博文,结合了《深度探索C++对象模型》,博文链接。
在g++中开启选项-fno-elide-constructors可以去掉任何返回值优化,则C obj = f(); 中,会发生两次拷贝,f()内栈内变量拷贝构造返回值临时变量,返回值临时变量拷贝构造obj变量。
这里有一份更详细点的文档...
https://en.cppreference.com/w/cpp/language/copy_elision
===============================================================
最后是关于push_back与emplace_back的测试。
T &&var1 = std::move(var2); 不存在移动拷贝。
void emplace_back( Args&&... args );通过std::forward<Args>(args)...实现。
#include <bits/stdc++.h>
using namespace std;
struct Stu {
int age;
Stu(const int age = ):age(age) {
cout << "construct" << endl;
}
Stu(const Stu& s){
cout << "copy construct" << endl;
}
Stu(Stu&& s) {
cout << "move construct" << endl;
};
Stu& operator = (const Stu& s) {
cout << "operator construct" << endl;
}
~Stu(){
cout << "destruct" << endl;
}
}; Stu Init(const int age) {
return Stu(age);
} int main() {
Stu s1();
cout << "1--------------" << endl;
Stu s2 = Init();
cout << "2--------------" << endl;
vector<Stu> ve;
ve.reserve();
cout << "3--------------" << endl;
ve.push_back(s1);
cout << "4--------------" << endl;
Stu&& ss = std::move(s1);
ve.push_back(std::move(s1));
cout << "5--------------" << endl;
ve.emplace_back(Stu());
cout << "6--------------" << endl;
ve.emplace_back();
return ;
} /*
construct
1--------------
construct
move construct
destruct
move construct
destruct
2--------------
3--------------
copy construct
4--------------
move construct
5--------------
construct
move construct
destruct
6--------------
construct
destruct
destruct
destruct
destruct
destruct
destruct
*/
copy elison & RVO & NRVO的更多相关文章
- C++基础知识--DAY4
今天主要讲的是类中除了构造器析构器以外的拷贝构造器,运算符重载等问题 首先是拷贝构造器 1. copy constructor(拷贝构造) 其也是构造器,其地位和constructor的地位是一样的 ...
- 第15课 右值引用(2)_std::move和移动语义
1. std::move (1)std::move的原型 template<typename T> typename remove_reference<T>::type& ...
- [c++11]右值引用、移动语义和完美转发
c++中引入了右值引用和移动语义,可以避免无谓的复制,提高程序性能.有点难理解,于是花时间整理一下自己的理解. 左值.右值 C++中所有的值都必然属于左值.右值二者之一.左值是指表达式结束后依然存在的 ...
- [转][c++11]我理解的右值引用、移动语义和完美转发
c++中引入了右值引用和移动语义,可以避免无谓的复制,提高程序性能.有点难理解,于是花时间整理一下自己的理解. 左值.右值 C++中所有的值都必然属于左值.右值二者之一.左值是指表达式结束后依然存在的 ...
- HEC-ResSim原文档
HEC-ResSim Reservoir System Simulation User's Manual Version 3.1 May 201 ...
- RVO和NRVO
返回值优化(Return Value Optimization,简称RVO),是这么一种优化机制:当函数需要返回一个对象的时候,如果自己创建一个临时对象用户返回,那么这个临时对象会消耗一个构造函数(C ...
- C++编译器优化技术:RVO、NRVO和复制省略
现代编译器缺省会使用RVO(return value optimization,返回值优化).NRVO(named return value optimization.命名返回值优化)和复制省略(Co ...
- C++中返回对象的情形及RVO
http://www.cnblogs.com/xkfz007/archive/2012/07/21/2602110.html 之前有文章介绍过临时对象和返回值优化RVO方面的问题.见此处. 在C++中 ...
- c++ rvo vs std::move
c++ rvo vs std::move To summarize, RVO is a compiler optimization technique, while std::move is just ...
随机推荐
- Redis详解(七)------ AOF 持久化
上一篇文章我们介绍了Redis的RDB持久化,RDB 持久化存在一个缺点是一定时间内做一次备份,如果redis意外down掉的话,就会丢失最后一次快照后的所有修改(数据有丢失).对于数据完整性要求很严 ...
- LOJ2537 PKUWC2018 Minimax 树形DP、线段树合并
传送门 题意:自己去看 首先可以知道,每一个点都有几率被选到,所以$i$与$V_i$的关系是确定了的. 所以我们只需要考虑每一个值的取到的概率. 很容易设计出一个$DP$:设$f_{i,j}$为在第$ ...
- 大话设计模式之工厂模式 C#
学无止境,精益求精 十年河东,十年河西,莫欺少年穷 学历代表你的过去,能力代表你的现在,学习代表你的将来 大话设计模式一书中第一个开讲的设计模式是简单工厂模式,关于简单工厂模式大家可参考鄙人的博客:代 ...
- Luogu P1477 [NOI2008]假面舞会
一道非常神奇的图论题解法无比新奇清新 我们首先把图分成三种情况: 有环的,此时答案一定是环长的因数(否则不能满足题意) 存在入度大于1的DAG图的 一棵树/一条链 很容易发现,最后一种情况想怎么取就怎 ...
- [Oracle]Oracle Fail Safe 与 SQLNET.AUTHENTICATION_SERVICES关系
现象: 在使用 OFS (Oracle Fail Safe)的环境中,把数据库的 SQLNET.AUTHENTICATION_SERVICES 从 NTS 改为 NONE之后,当从 Oracle Fa ...
- 通用漏洞评估方法CVSS 3.0 计算公式及说明
CVSS 3.0 计算公式及说明 一.基础评价 1. 基础评价公式为: 当 影响度分值 <= 0: 基础分值 = 0 当 0 < 影响度分值 + 可利用度分值 < 10: 作用域 = ...
- CF1146 Forethought Future Cup Elimination Round Tutorial
CF1146 Forethought Future Cup Elimination Round Tutorial 叮,守夜冠军卡 https://codeforces.com/blog/entry/6 ...
- 时间复杂度O(n^2)和O(nlog n)差距有多大?
0. 时间复杂度 接触到算法的小伙伴们都会知道时间复杂度(Time Complexity)的概念,这里先放出(渐进)时间复杂度的定义: 假设问题规模是\(n\),算法中基本操作重复执行的次数是\(n\ ...
- Linux下环境变量配置方法梳理(.bash_profile和.bashrc的区别)
在linux系统下,如果下载并安装了应用程序,在启动时很有可能在键入它的名称时出现"command not found"的提示内容.如果每次都到安装目标文件夹内,找到可执行文件来进 ...
- Jenkins新建项目中源码管理Repository URL使用Git报错:Failed to connect to repository : Command "git ls-remote -h......
之前部署了Gitlab+Gerrit+Jenkins持续集成环境,但在Jenkins中新建项目的源码管理"Repository URL"中添加git地址环节出现了问题,信息为&qu ...