不写了,这个东西越研究就越多,滚雪球一样,暂时撤退。。
感觉现在C++水平也算是勉强入门了,不再是C with STL了(逃)。
不过很多东西诸如compiling, parsing, linking and symbol table, etc
都慢慢有了一些细微的认知。。。

参考来源: (主要3\4\5)
[1].http://zh.cppreference.com/w/cpp/language/copy_elision
[2].http://blog.csdn.net/PeerlessBloom/article/details/77096198
[3].https://www.ibm.com/developerworks/community/blogs/5894415f-be62-4bc0-81c5-3956e82276f3/entry/RVO_V_S_std_move?lang=en
[4].https://stackoverflow.com/questions/48160292/in-c11-does-returning-a-stdstring-in-a-function-move-or-copy-it
[5].https://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization/12953129#12953129
[6].https://stackoverflow.com/questions/4986673/c11-rvalues-and-move-semantics-confusion-return-statement?lq=1
[7].https://msdn.microsoft.com/zh-cn/library/ms364057(v=vs.80).aspx

From [6] we may take a little cognizance of rvalue and move semantics, and remember:

The following four figures are from [2], (Mr.peerlessbloom)
(but I do not know how to remove the watermark,
apologize here…………………………………………………………………………..!!!!!!!!)

…………………………………………………………………………………………………………………..
Copy elision is an optimization implemented by most compilers to prevent extra (potentially expensive) copies in certain situations. It makes returning by value or pass-by-value feasible in practice (restrictions apply).

When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the copy/move constructor and/or destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization. This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

1) in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value.

2) in a throw-expression, when the operand is the name of a non-volatile automatic object (other than a function or catch-clause parameter) whose scope does not extend beyond the end of the innermost enclosing try-block (if there is one), the copy/move operation from the operand to the exception object can be omitted by constructing the automatic object directly into the exception object.

3) when a temporary class object that has not been bound to a reference would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move

4) when the exception-declaration of an exception handler declares an object of the same type (except for cv-qualification) as the exception object, the copy/move operation can be omitted by treating the exception-declaration as an alias for the exception object if the meaning of the program will be unchanged except for the execution of constructors and destructors for the object declared by the exception-declaration.

Ex.

A MyMethod (B &var)
{A retVal;retVal.member = var.value + bar(var);return retVal;
}valA = MyMethod(valB);

VC++ compiler may have optimize the code and it may have the following structure:

A MyMethod (A &_hiddenArg, B &var)
{A retVal;
   retVal.A::A(); // constructor for retValretVal.member = var.value + bar(var);
   _hiddenArg.A::A(retVal);  // the copy constructor for Areturn;
retVal.A::~A();  // destructor for retVal}

…………………………………………………………………………………………………………………….
(N)RVO : (Named) Return Value Optimization
RVO, is a compiler optimization technique that allows the compiler to construct the return value of a function at the call site.

The mechanism of RVO:

This diagram is a normal function stack frame. If we call the function without RVO, the function simply allocates the space for the return in its own frame. The process is demonstrated in the following diagram:

What will happen if we call the function with RVO?

We can find that RVO uses parent stack frame (or an arbitrary block of memory) to avoid copying. So, if we add if-else branch, the compiler doesn’t know which return value to put.

太多了,到此为止。

Intro to Copy Elision and (N)RVO相关推荐

  1. 浅谈C++11标准中的复制省略(copy elision,也叫RVO返回值优化)

    严正声明:本文系作者davidhopper原创,未经许可,不得转载. C++11以及之后的C++14.17标准均提出一项编译优化技术:复制省略(copy elision,也称复制消除),另外还有RVO ...

  2. C++中的RVO、NRVO与Copy Elision

    RVO: Return Value Optimization NRVO: Named Return Value Optimization RVO 和 NRVO 自C++98时代就已存在,即当函数按值返 ...

  3. 一道面试题:你了解哪些编译器优化行为?知道Copy elision 、RVO吗?

    C++11以后,g++ 编译器默认开启复制省略(copy elision)选项,可以在以值语义传递对象时避免触发复制.移动构造函数.copy elision 主要发生在两个场景: 函数返回的是值语义时 ...

  4. c++的复制省略(copy elision)

    学习 A simple C++11 Thread Pool 时,发现函数返回了std::future,而std::future的拷贝构造和拷贝赋值都是delete的,感觉有点怪,查了一下,看到 编译器 ...

  5. 有保证的复制消除(Guaranteed Copy Elision)

    作者:Jonas Devlieghere 原文地址:https://jonasdevlieghere.com/guaranteed-copy-elision/ 新的 C++ 17 标准带来了很多令人兴 ...

  6. C++ 的 Copy Elision

    Copy Elision 我们都讨厌 copy ? 如何避免 copy ? 强制性 Copy Elision 非强制性 Copy Elision 总结 我们都讨厌 copy ? 关于 copy (拷贝 ...

  7. C++ - Copy Elision

    Copy Elision C++11/14/17编译器优化:省略不必要的拷贝 至少包括以下两项内容: 返回值优化(RVO),即通过将返回值所占空间的分配地点从被调用端转移至调用端的手段来避免拷贝操作. ...

  8. C++17之省略不必要的拷贝Copy Elision

    从C++发展历史看来,c++ 17引入了一个规则,要求在满足一定的条件下避免对象的复制,这在以前是可选的.C++17中一个叫做强制复制省略的技术就是基于这个观点的. 至少包括以下两项内容: 1. 返回 ...

  9. C++编程法则365条一天一条(358)copy elision(返回值优化NVO和具名返回值优化NRVO)

    文章目录 强制编译器实现的优化 非强制实现优化 参考:https://en.cppreference.com/w/cpp/language/copy_elision Elision 是省略.删节或者忽 ...

最新文章

  1. 一个c语言构造函数调用的问题(有趣)
  2. 如何使用SAP UI5 web Component的React框架的柱状图和折线图
  3. 全国计算机等级考试题库二级C操作题100套(第10套)
  4. CodeForces - 1144F搜索+简单图论
  5. Django简介以及安装
  6. 温州近10%的网站存在高危安全漏洞
  7. 关于static继承的问题
  8. PDF加密文件如何解密(无密码)
  9. 一文带你深入了解大数据服务
  10. 使用Python连接crossbar.io 报错 could not create serializer for “cbor“ (available: [‘json‘])
  11. Excel如何分别提取出数值整数部分和小数部分
  12. IE8和IE7下js的兼容性问题
  13. 【SSD】自动化测试框架
  14. 5-40 奥运排行榜 (25分)
  15. URP——着色器和材质——简单光照 Simple Lit
  16. 厦门有哪些靠谱的互联网公司
  17. 韭菜投资ABC:如何判断当前指数便宜还是贵(PE百分位法)
  18. 智能仪器及智能仪表设计的新突破--将实时安卓操作系统(RTAndroid)引入到智能仪器及智能仪表设计
  19. 合宙昆仑镜LCD驱动测试
  20. 【错误处理】 org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

热门文章

  1. axure 授权码,试用期过了也可以用
  2. 解决iview weapp的i-input组件在微信开发者工具中不能输入值的问题
  3. mac打开airplay(隔空播放)
  4. js距离单位换算_js实现单位互换px/cm/mm篇
  5. 计算机技能大赛 英语,计算机科学与技术学院英语技能大赛圆满结束
  6. Android Studio+Emulator启动黑屏解决方法
  7. 低信噪比MIMO SC-FDE系统中信道估计的研究与实现
  8. 【Akka】Akka 学习 akka 两本书的读后感
  9. 吴恩达 深度学习 序列模型 第一周 编程作业二 字符级别语言模型项目 总结
  10. 童年记忆中的优良环境