加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 分配给在表达式右侧移动的变量

发布时间:2020-12-16 10:07:47 所属栏目:百科 来源:网络整理
导读:这是未定义的行为吗? std::unique_ptrT p = some_function();p = some_other_function(std::move(p)); 我读过http://en.cppreference.com/w/cpp/language/eval_order,但没有找到明确的答案. 解决方法 如果您搜索任务,那么您将找到规则8,仅提及内置赋值运算
这是未定义的行为吗?

std::unique_ptr<T> p = some_function();

p = some_other_function(std::move(p));

我读过http://en.cppreference.com/w/cpp/language/eval_order,但没有找到明确的答案.

解决方法

如果您搜索任务,那么您将找到规则8,仅提及内置赋值运算符,std::unique_ptr执行 overload the assignment operator.

但由于操作符超载,声明

p = some_other_function(std::move(p));

实际上是一个函数调用:

p.operator=(some_other_function(std::move(p)));

这使它成为规则3的一部分.这使得它定义明确(因为std :: move(p)必须在调用some_other_function之前完成,而some_other_function必须在调用p.operator =之前完成).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读