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++的更多相关文章

  1. C++ Copy Elision

    故事得从 copy/move constructor 说起: The default constructor (12.1), copy constructor and copy assignment ...

  2. C++的Copy Elision导致的奇怪问题

    最近写设计模式作业的时候, 有一个作业是实现装饰器模式 (Decorator Pattern), 由于我不会 Java, 所以只能用 C++ 来实现 在这个背景下, 会有简单(表意)的几个类, 如下: ...

  3. copy elision

    http://book.51cto.com/art/200810/93007.htm 1.2.2  数据传送指令 mov:数据移动.第一个参数是目的,第二个参数是来源.在C语言中相当于赋值号.这是最广 ...

  4. copy elison & RVO & NRVO

    蓝色的博文 To summarize, RVO is a compiler optimization technique, while std::move is just an rvalue cast ...

  5. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  6. 翻译「C++ Rvalue References Explained」C++右值引用详解 Part6:Move语义和编译器优化

    本文为第六部分,目录请参阅概述部分:http://www.cnblogs.com/harrywong/p/cpp-rvalue-references-explained-introduction.ht ...

  7. move和转发

    总的来说C++09跟C++98相比的变化是极其重大的.这个变化体现在三个方面,一个是形式上的变化,即在编码形式层面的支持,也就是对应我们所谓的编程范式(paradigm).C++09不会引入新的编程范 ...

  8. C++临时对象以及针对其进行的优化

    C++临时对象以及针对其进行的优化 C++中真正的临时对象是看不见的,它们不出现在你的源代码中. 那么什么时候回产生临时对象呢?主要是三个时刻: 产生临时对象的三个时刻: 用构造函数作为隐式类型转换函 ...

  9. Google C++ 代码规范

    Google C++ Style Guide   Table of Contents Header Files Self-contained Headers The #define Guard For ...

随机推荐

  1. Excel 读写

    一.环境准备:pom.xml 导入依赖 poi-ooxml <dependencies> <dependency> <groupId>org.apache.poi& ...

  2. aardio 开发桌面应用,这几点必须要掌握!

    1. 前言 大家好,我是安果! 上一篇文章写到可以通过 aardio 结合 Python 开发桌面应用,有些小伙伴后台给我留言,说 Aardio 资料太少,希望我能补充一些实用的功能 实用 | 利用 ...

  3. IIS设置URL重写,实现页面的跳转的重定向方法

    默认IIS是不提供URL重写模块的. 请注意,不要将IIS默认的HTTP重定向理解为url重写. 安装url重写模块 url重写,是要从iis的应用市场下载url重写组件才可以的. URL重写工具的下 ...

  4. [luogu7418]Counting Graphs P

    参考[luogu7417],同样求出最短路,得到二元组$(x,y)$并排序,记$tot_{(x,y)}$为$(x,y)$的数量 其中所给的两个条件,即分别要求: 1.$(x,y)$只能和$(x\pm ...

  5. [cf461D]Appleman and Complicated Task

    假设该矩形是aij,那么有a(i,j)=a(i-1,j-1)^a(i-1,j+1)^a(i-2,j),不断递归下去可以发现a(i,j)=a(1,y-x+1)^a(1,y-x+3)^--^a(1,x+y ...

  6. [bzoj1483]梦幻布丁

    对于每一个颜色用一个链表存储,并记录下:1.当前某种颜色的真实颜色:2.这种颜色的数量(用于启发式合并的判断):3.当前答案(即有几段),然后对于每一个操作简单处理一下就行了. 1 #include& ...

  7. [hdu6601]Keen On Everything But Triangle

    有两个结论:1.排序后,答案一定是连续的三个数:2.当序列长度超过44一定有三个相同的数(因为即使该序列是斐波那契数列,此时也超过了1e9),然后用主席树等数据结构(略卡常,建议主席树)来维护前45大 ...

  8. [bzoj4945]游戏

    暴力枚举$2^{d}$表示这d个点中一定不选A或一定不选B(那么就包含了所有情况),然后就对原图跑2-sat即可注意一个细节,如果某一条限制中初始点不合法,就不用管了:如果最终点不合法,那么相当于初始 ...

  9. AOP声明式事务

    1.spring-dao.xml修改 参考上面工程配置 <?xml version="1.0" encoding="UTF-8"?> <bea ...

  10. Python迭代器生成器与生成式

    Python迭代器生成器与生成式 什么是迭代 迭代是重复反馈过程的活动,其目的通常是为了逼近所需目标或结果.每一次对过程的重复称为一次"迭代",而每一次迭代得到的结果会作为下一次迭 ...