Tip of the Week #24: Copies, Abbrv

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 77TotW 55

One Name, No Copy; Two Names, Two Copies

当评估是否在任何给定范围内是否有副本(包括触发RVO的情况)时,请检查你的数据被多少个变量名引用。

在任何时候如果一个数据有两份副本的话,那么你一定拥有两个有效的名字来引用这两个副本。其他情况下编译器会负责删除副本。

Examples

让我们来看下面这个例子,看看实际情况下是如何运作的:

std::string build();

std::string foo(std::string arg) {
  return arg;  // no copying here, only one name for the data “arg”.
}

void bar() {
  std::string local = build();  // only 1 instance -- only 1 name

  // no copying, a reference won’t incur a copy
  std::string& local_ref = local;

  // one copy operation, there are now two named collections of data.
  std::string second = foo(local);
}

大多数情况下这都不重要,确保您的代码可读并且一致是非常重要的。而不是担心副本拷贝和性能。一如既往,不要过早优化。但是如果你发现从头开始编写代码的话你可以提供一个更加干净和一致的API返回值,不要因为它可能会带来拷贝而妥协,你在十年前用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.
最后一段翻译的不太好,如果有更好的翻译,请告知我。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值