Copy elision in C++
Copy elision (or Copy omission) is a compiler optimization technique that avoids unnecessary copying of objects. Now a days, almost every compiler uses it.
Let us understand it with the help of an example.
1 #include <iostream>
2 using namespace std;
3
4 class B
5 {
6 public:
7 B(const char* str = "\0") //default constructor
8 {
9 cout << "Constructor called" << endl;
10 }
11
12 B(const B &b) //copy constructor
13 {
14 cout << "Copy constructor called" << endl;
15 }
16 };
17
18 int main()
19 {
20 B ob = "copy me";
21 return 0;
22 }
The output of above program is: Constructor called
Why copy constructor is not called?
According to theory, when the object “ob” is being constructed, one argument constructor is used to convert “copy me” to a temporary object & that temporary object is copied to the object “ob”.
So the statement
B ob = "copy me";
should be broken down by the compiler as
B ob = B("copy me");
However, most of the C++ compilers avoid such overheads of creating a temporary object & then copying it.
The modern compilers break down the statement
B ob = "copy me"; //copy initialization
as
B ob("copy me"); //direct initialization
and thus eliding call to copy constructor.
However, if we still want to ensure that the compiler doesn’t elide the call to copy constructor [disable the copy elision], we can compile the program using “-fno-elide-constructors” option with g++ and see the output as following:
root:~$ g++ copy_elision.cpp -fno-elide-constructors
root:~$ ./a.out
Constructor called
Copy constructor called
If “-fno-elide-constructors” option is used, first default constructor is called to create a temporary object, then copy constructor is called to copy the temporary object to ob.
Reference: http://en.wikipedia.org/wiki/Copy_elision
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-26 11:13:29
Copy elision in C++的更多相关文章
- C++ Copy Elision
故事得从 copy/move constructor 说起: The default constructor (12.1), copy constructor and copy assignment ...
- C++的Copy Elision导致的奇怪问题
最近写设计模式作业的时候, 有一个作业是实现装饰器模式 (Decorator Pattern), 由于我不会 Java, 所以只能用 C++ 来实现 在这个背景下, 会有简单(表意)的几个类, 如下: ...
- copy elision
http://book.51cto.com/art/200810/93007.htm 1.2.2 数据传送指令 mov:数据移动.第一个参数是目的,第二个参数是来源.在C语言中相当于赋值号.这是最广 ...
- copy elison & RVO & NRVO
蓝色的博文 To summarize, RVO is a compiler optimization technique, while std::move is just an rvalue cast ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- 翻译「C++ Rvalue References Explained」C++右值引用详解 Part6:Move语义和编译器优化
本文为第六部分,目录请参阅概述部分:http://www.cnblogs.com/harrywong/p/cpp-rvalue-references-explained-introduction.ht ...
- move和转发
总的来说C++09跟C++98相比的变化是极其重大的.这个变化体现在三个方面,一个是形式上的变化,即在编码形式层面的支持,也就是对应我们所谓的编程范式(paradigm).C++09不会引入新的编程范 ...
- C++临时对象以及针对其进行的优化
C++临时对象以及针对其进行的优化 C++中真正的临时对象是看不见的,它们不出现在你的源代码中. 那么什么时候回产生临时对象呢?主要是三个时刻: 产生临时对象的三个时刻: 用构造函数作为隐式类型转换函 ...
- Google C++ 代码规范
Google C++ Style Guide Table of Contents Header Files Self-contained Headers The #define Guard For ...
随机推荐
- 大白话讲解如何解决HttpServletRequest的请求参数只能读取一次的问题
大家在开发过程中,可能会遇到对请求参数做下处理的场景,比如读取上送的参数中看调用方上送的系统编号是否是白名单里面的(更多的会用request中获取IP地址判断).需要对请求方上送的参数进行大小写转换或 ...
- uni-app视频组件设置圆角
无法实现,建议写个image在中间位置加个播放按钮,点击播放跳转新页面只需要在跳转参数里面把视频链接加上,在onLoad里面获取视频链接,自动播放视频,很多app目前都是这样做的,关闭页面后视频会自动 ...
- axios 基于拦截器的取消(重复)请求
axios 基于拦截器的取消(重复)请求 // 添加请求拦截器 axios.interceptors.request.use((config) => { // 准备发请求之前, 取消未完成的请求 ...
- webpack 之 js语法检查eslint
webpack 之 js语法检查eslint // 用来拼接绝对路径的方法 const {resolve} = require('path') const HtmlWebpackPlugin = re ...
- Go defer 原理和源码剖析
Go 语言中有一个非常有用的保留字 defer,它可以调用一个函数,该函数的执行被推迟到包裹它的函数返回时执行. defer 语句调用的函数,要么是因为包裹它的函数执行了 return 语句,到达了函 ...
- Django 项目配置拆分独立
目录 一.创建配置目录 二.创建基础配置文件 三.创建各个环境的配置 四.调整settings.py 五.程序使用 六.目录结构 Django 项目中,我们默认的配置是都在 settings.py 文 ...
- myeclipse trial expired暂时解决办法
运行以下程序,生成key: import java.io.*; public class MyEclipseGen { private static final String LL = "D ...
- [loj3527]地牢游戏
当英雄能力值$\ge 10^{7}$时,即能战胜所有敌人,简单预处理即可 若英雄能力值在$[2^{k},2^{k+1})$中,对敌人分类讨论: 1.若$s_{i}\le 2^{k}$,其必然会战胜这些 ...
- ound interface org.elasticsearch.common.bytes.BytesReference, but class was expected
es得版本和本地项目不一致.. 配置es版本,现在使用得是5.2得版本,可是 maven上看到 elasticsearch-rest-high-level-client 最低也得6版本.下载安装高版本 ...
- Codeforces 1225G - To Make 1(bitset+状压 dp+找性质)
Codeforces 题目传送门 & 洛谷题目传送门 还是做题做太少了啊--碰到这种题一点感觉都没有-- 首先我们来证明一件事情,那就是存在一种合并方式 \(\Leftrightarrow\) ...