c++ vector 赋值_C++核心准则C.62:保证拷贝赋值时的自我赋值的安全而不是防止它

a45106eaf2ae067142e44738e8b081b1.png

C.62: Make copy assignment safe for self-assignment
C.62:保证拷贝赋值对自我赋值安全

Reason(原因)

If x = x changes the value of x, people will be surprised and bad errors will occur (often including leaks).

如果x=x改变了x的值,人们会觉得很奇怪,同时也会发生很不好的错误。(通常会包含泄露)

Example(示例)

The standard-library containers handle self-assignment elegantly and efficiently:

标准库容器处理自我赋值的方式优雅且高效:

std::vector v = {3, 1, 4, 1, 5, 9};v = v;// the value of v is still {3, 1, 4, 1, 5, 9}

Note(注意)

The default assignment generated from members that handle self-assignment correctly handles self-assignment.

产生于正确处理了自我赋值的成员的默认的赋值操作会处理自我赋值问题。

struct Bar {    vector> v;    map m;    string s;};Bar b;// ...b = b;   // correct and efficient

Note(注意)

You can handle self-assignment by explicitly testing for self-assignment, but often it is faster and more elegant to cope without such a test (e.g., using swap).

你可以通过明确地对自我赋值进行检查的方式处理自我赋值,但是通常不使用上述检查的处理方式(例如使用swap)都会更快,更优雅。

class Foo {    string s;    int i;public:    Foo& operator=(const Foo& a);    // ...};Foo& Foo::operator=(const Foo& a)   // OK, but there is a cost{    if (this == &a) return *this;    s = a.s;    i = a.i;    return *this;}

This is obviously safe and apparently efficient. However, what if we do one self-assignment per million assignments? That's about a million redundant tests (but since the answer is essentially always the same, the computer's branch predictor will guess right essentially every time). Consider:

这种做法显然安全并且看起来高效。但是如果在一百万次赋值中发生一次自我赋值的情况下会怎么样呢?大概有一百万次多余的检查(但是由于本质上结果总是一样的,计算机的分支预测会每次都猜对)。考虑下面的代码:

Foo& Foo::operator=(const Foo& a)   // simpler, and probably much better{    s = a.s;    i = a.i;    return *this;}

std::string is safe for self-assignment and so are int. All the cost is carried by the (rare) case of self-assignment.

std::string对自我赋值安全,int也是。所有的代价都来自(极少)发生的自我赋值。

Enforcement(实施建议)

(Simple) Assignment operators should not contain the pattern if (this == &a) return *this; ???

(简单)赋值运算符不应该包含以下的检查:if (this == &a) return *this;


觉得本文有帮助?请分享给更多人。

更多精彩文章请关注微信公众号【面向对象思考】!

面向对象开发,面向对象思考!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值