前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则C.65:让移动操作对自赋值安全

C++核心准则C.65:让移动操作对自赋值安全

作者头像
面向对象思考
发布2020-03-25 16:16:42
3060
发布2020-03-25 16:16:42
举报

C.65: Make move assignment safe for self-assignment C.65:让移动操作对自赋值安全

Reason(原因)

If x = x changes the value of x, people will be surprised and bad errors may occur. However, people don't usually directly write a self-assignment that turn into a move, but it can occur. However, std::swap is implemented using move operations so if you accidentally do swap(a, b) where a and b refer to the same object, failing to handle self-move could be a serious and subtle error.

如果x=x改变了x的值,人们会感到诧异而且有可能发生严重的错误。虽然人们一般不会直接在移动操作中使用自赋值,它还是会发生。但是由于std::swap被实现为使用移动操作,如果你意外地调用了swap(a,b)而a和b参照了同一个对象,如果没有处理好自赋值的话,可能会发生严重且不易发现的错误。

Example(示例)

class Foo {
    string s;
    int i;
public:
    Foo& operator=(Foo&& a);
    // ...
};

Foo& Foo::operator=(Foo&& a) noexcept  // OK, but there is a cost
{
    if (this == &a) return *this;  // this line is redundant
    s = std::move(a.s);
    i = a.i;
    return *this;
}

The one-in-a-million argument againstif (this == &a) return *this; tests from the discussion of self-assignment is even more relevant for self-move.

违反自我赋值检查的概率只有百万分之一(数据只要领会精神就好,译者注);避免自我赋值的讨论和自我移动的关系更加密切。

Note(注意)

There is no known general way of avoiding an if (this == &a) return *this; test for a move assignment and still get a correct answer (i.e., after x = x the value of x is unchanged).

没有避免使用if(this==&a)return *this;操作的普遍办法。检查移动赋值的方法仍然会得出正确的结果(例如在x=x之后x的值不会改变)。

Note(注意)

The ISO standard guarantees only a "valid but unspecified" state for the standard-library containers. Apparently this has not been a problem in about 10 years of experimental and production use. Please contact the editors if you find a counter example. The rule here is more caution and insists on complete safety.

ISO标准只为标准库容易保证了一个“合法但未定义”的状态。看起来在10多年的经验和产品应用中没有成为问题。如果你遇到了反例请联系编辑。本规则更为谨慎并坚持做到完全安全。

Example(示例)

Here is a way to move a pointer without a test (imagine it as code in the implementation a move assignment):

这里有一个不检查就移动指针的方法(将其想象为实现移动赋值代码中的一部分):

// move from other.ptr to this->ptr
T* temp = other.ptr;
other.ptr = nullptr;
delete ptr;
ptr = temp;

译者注

如果other和this是同一个对象,other.ptr=nullptr也会同时将this.ptr置空,导致下面的delete ptr不起作用。这样就保证了自我移动时的安全。

这种做法太难理解了。

Enforcement(示例)

  • (Moderate) In the case of self-assignment, a move assignment operator should not leave the object holding pointer members that have been deleted or set to nullptr.
  • (中等)在自赋值的情况下,移动操作运算符应该避免对象的指针成员指向的对象被销毁或者该指针成员被置空。
  • (Not enforceable) Look at the use of standard-library container types (incl. string) and consider them safe for ordinary (not life-critical) uses.
  • (无法实施)找到使用标准库容器类型(包括string),认为它们在通常用途(不是声明周期敏感)时安全的。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c65-make-move-assignment-safe-for-self-assignment


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

关注【面向对象思考】轻松学习每一天!

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-01-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档