Tip of the Week #24: Copies, Abbrv
Originally posted as TotW #24 on Nov 26, 2012 by Titus Winters, (titus@google.com) and Chandler Carruth (chandlerc@google.com) “To copy others is necessary, but to copy oneself is pathetic.” - Pablo Picasso
注意: 有关名称计数和副本与移动的指导,请参见TotW 77和TotW 55
One Name, No Copy; Two Names, Two Copies
当评估是否在任何给定范围内是否有副本(包括触发RVO
的情况)时,请检查你的数据被多少个变量名引用。
在任何时候如果一个数据有两份副本的话,那么你一定拥有两个有效的名字来引用这两个副本。其他情况下编译器会负责删除副本。
Examples
让我们来看下面这个例子,看看实际情况下是如何运作的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
C++
学习过的所有内容都是错误的。
As always: profile before you optimize. But, if you find yourself writing code from scratch – and can provide a clean and consistent API that returns its values – don’t discount code that seems like it would make copies: everything you learned about copies in C++ a decade ago is wrong. 最后一段翻译的不太好,如果有更好的翻译,请告知我。