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 ...
随机推荐
- 03 | 变量的解构赋值 | es6
变量的解构赋值 数组的解构赋值 基本用法 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring). 以前,为变量赋值,只能直接指定值. let a ...
- redis批量操作
由于redis没有批量删除命令,所以借助xargs redis-cli -h 127.0.0.1 keys testdata_2018* |xargs redis-cli -h 127.0.0.1 d ...
- 求求你们了,别再写满屏的 if/ else 了!
为什么我们写的代码都是 if-else? 程序员想必都经历过这样的场景:刚开始自己写的代码很简洁,逻辑清晰,函数精简,没有一个 if-else,可随着代码逻辑不断完善和业务的瞬息万变:比如需要对入参进 ...
- 大一C语言学习笔记(7)---指针篇--什么是指针?什么是指针变量?取地址符“&”的作用是什么?地址运算符“*”的作用是什么,怎么理解两者?
"指针是C语言的灵魂"这句话一开始我没怎么明白,现在接触了指针,终于知道为什么这么说了,因为....难,真难:下面说一下我对这句话的见解: C语言拥有着其他语言所没有的特性---直 ...
- 解决tomcat的404问题
遇到的问题 点击startup.bat启动tomcat启动成功,但在网页上输入local:8080却显示Access Error: 404 -- Not Found Cannot locate doc ...
- Effective C++ 总结笔记(一)
一.让自己习惯C++ 01.视C++为一个语言联邦 c++是多重范型编程 语言,视c++包括4种次语言: 1:C 2:Object-Oreinted C++: 3:Template C++: 4:ST ...
- 调用企业微信API拨打紧急通知电话
# 前提条件:企业信息:行业类型必须属于"医疗"大类,客服反馈说目前不支持其他行业# 准备工作:https://work.weixin.qq.com/api/doc/90000/9 ...
- c语言1左移32位(1<<32)是多少,左移-1位呢
C语言中 << 是逻辑移位,不是循环移位.1 左移 32 位后为 0,左移 -1 位实际是左移 255 位(互补),当然也是0.这种问题可以写一段小程序,单步执行,看一下每一步的结果.先说 ...
- [cf1340D]Nastya and Time Machine
记$deg_{i}$为$i$的度数,简单分类讨论可得答案下限为$\max_{i=1}^{n}deg_{i}$ 另一方面,此下限是可以取到的,构造方法较多,这里给一个巧妙一些的做法-- 对其以dfs(儿 ...
- Windows系统及硬件信息读取
Windows桌面端开发常常会需要读取系统信息或硬件信息作为用户标识,比如用于确认该设配是否已经激活程序.也可以使用随机生成的UUID来作为唯一标识,但是如果重装系统或重装软件都有可能导致标识丢失,因 ...