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 ...
随机推荐
- 2020美亚团体—Daniel篇
Daniel的桌上计算机的哈希值(SHA-256)是甚么? 通过取证大师计算 SHA-256值为 07DD40CF28603F421F3A09CD38F1C8AA40A2AC4BFB46ECF8299 ...
- Java测试开发--Comparable和Comparator接口(五)
Comparable 简介Comparable 是排序接口.若一个类实现了Comparable接口,就意味着"该类支持排序".此外,"实现Comparable接口的类的对 ...
- 你会用ES6,那倒是用啊!
leader的吐槽大会(在代码评审中发现很多地方还是采用ES5的写法,也不是说用ES5写法不行,会有BUG,只是造成代码量增多,可读性变差而已.) ps:ES5之后的JS语法统称ES6!!! 一.关于 ...
- Python | Python常用函数、方法示例总结(API)
目录 前言 1. 运算相关 2. Sring与数字 3. 列表相关 4. 集合相关 5. 序列化类型 6. 字典相关 7. 输入输出 8. 文件相关 9. json模块 10. unittest测试模 ...
- Unable to unwrap data, invalid status [CLOSED]-服务端webSocket报错
一.问题由来 现在的项目中在使用webSocket这门技术,主要用来在服务端和客户端进行实时的数据传输,因为需要及时的进行响应,所以才没有使用http请求的方式, 而是使用socket的方式,这样可以 ...
- 基于Netty4手把手实现一个带注册中心和注解的Dubbo框架
阅读这篇文章之前,建议先阅读和这篇文章关联的内容. 1. 详细剖析分布式微服务架构下网络通信的底层实现原理(图解) 2. (年薪60W的技巧)工作了5年,你真的理解Netty以及为什么要用吗?(深度干 ...
- 如何利用 JuiceFS 的性能工具做文件系统分析和调优
JuiceFS 是一款面向云原生环境设计的高性能 POSIX 文件系统,在 AGPL v3.0 开源协议下发布.作为一个云上的分布式文件系统,任何存入 JuiceFS 的数据都会按照一定规则拆分成数据 ...
- 一次奇怪的的bug排查过程
公司对底层基础库进行了重构,线上稳定跑了几天,在查看订单系统的log时,有几条error信息非常的奇怪, orderID:80320180 statemachine error: no event [ ...
- #pragma warning(disable:4996)
VS使用中常常会遇到一些过时或者不安全的函数,编译会报错: 错误 C4996 'sscanf': This function or variable may be unsafe. Consider u ...
- FESTUNG模型介绍—1.对流方程求解
FESTUNG模型介绍-1.对流方程求解 1. 控制方程 对流问题中,控制方程表达式为 \[\partial_t C + \partial_x (u^1 C) + \partial_y (u^2 C) ...