Item 20: Facilitate the return value optimization.(More Effective C++)

编程知识 更新时间:2023-04-06 01:09:42

The rules for C++ allow compilers to optimize temporary objects out of existence. As a result, if you call operator* in a context like this,

Rational a = 10; 
Rational b(1, 2); 
Rational c = a * b;                          // operator* is called here 

your compilers are allowed to eliminate both the temporary inside operator* and the temporary returned by operator*. They can construct the object defined by the return expression inside the memory allotted for the object c. If your compilers do this, the total cost of temporary objects as a result of your calling operator* is zero: no temporaries are created. Instead, you pay for only one constructor call — the one to create c. Furthermore, you can't do any better than this, because c is a named object, and named objects can't be eliminated

转载于:https://wwwblogs/zhtf2014/archive/2012/03/09/2387538.html

更多推荐

Item 20: Facilitate the return value optimization.(More Effective C++)

本文发布于:2023-04-06 01:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/168b030cd88681979d9e73724edcdff4.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Facilitate   Item   Effective   optimization   return

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 47970文章数
  • 14阅读数
  • 0评论数