为什么C ++移动语义会使源代码结构化? [英] Why does C++ move semantics leave the source constructed?

查看:64
本文介绍了为什么C ++移动语义会使源代码结构化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11中,引入了移动语义,它通过两个特殊成员实现:移动构造函数和移动赋值。这两个操作都会使从其移出对象被构造。

In C++11, "move semantics" was introduced, implemented via the two special members: move constructor and move assignment. Both of these operations leave the moved-from object constructed.

将源置于破坏状态会更好吗?

Wouldn't it have been better to leave the source in a destructed state? Isn't the only thing you can do with a moved-from object is destruct it anyway?

推荐答案

在宇宙的宇宙中,不是唯一可以对它进行破坏的事情吗?移动操作有四种可能性:

In the "universe of move operations" there are four possibilities:

target          source
  is            is left
----------------------------------------------------------
constructed <-- constructed  // C++11 -- move construction
constructed <-- destructed
assigned    <-- constructed  // C++11 -- move assignment
assigned    <-- destructed

每个操作有用!仅 std :: vector< T> :: insert 可以利用前三个。不过请注意:

Each of these operations is useful! std::vector<T>::insert alone could make use of the first three. Though note:


  • 第二个和第四个源不应具有自动,静态或线程存储持续时间。源的存储持续时间必须是动态的,否则编译器将在已销毁的对象上调用销毁器。而且不,编译器无法(通常)无法跟踪对象是否已从以下位置移出:

X x1, x2;
if (sometimes)
{
    x1 = std::move(x2);
}
// Is x2 moved-from here?







  • 通过简单地在操作后在源上手动调用析构函数,可以分别通过第1和第3个模拟第2和第4个。


    • The 2nd and 4th can be emulated by the 1st and 3rd respectively, by simply manually calling the destructor on the source after the operation.

      第1和第3个分别是关键。 std :: swap std :: sort 之类的算法通常都需要这两个操作。这些算法不需要破坏其任何输入对象—。

      The 1st and 3rd are crucial. Algorithms such as std::swap and std::sort regularly need both of these operations. These algorithms do not need to destruct any of their input objects — only change their values.

      有了这些知识,在2001-2002年的时间范围内,我将精力集中在留下了源代码的两个操作,因为这两个操作对C ++ 98的影响最大(正)。当时我知道,如果不减少这个项目的野心,它将永远不会成功。即使缩减,也过于雄心勃勃,无法成功。

      Armed with this knowledge, in the 2001-2002 time frame I focused my efforts on the two operations that left their source constructed because these two operations would have the largest (positive) impact on what was then C++98. I knew at the time that if I did not curtail the ambitions of this project, that it would never succeed. Even curtailed, it was borderline too-ambitious to succeed.

      这种缩减在原始移动语义建议在破坏性移动语义部分下。

      This curtailment is acknowledged in the original move semantics proposal under the section titled "Destructive move semantics".


      最后,我们只是放弃了这个麻烦,因为没有得到足够的
      收益。但是,当前的提议不禁止将来
      的破坏性移动语义。如果有人希望携带该手电筒,可以在此
      提案中概述的无损移动语义的基础上,另外
      来完成。

      In the end, we simply gave up on this as too much pain for not enough gain. However the current proposal does not prohibit destructive move semantics in the future. It could be done in addition to the non-destructive move semantics outlined in this proposal should someone wish to carry that torch.

      有关使用移出的对象可以做什么的更多详细信息,请参见 https://stackoverflow.com/ a / 7028318/576911

      For more details on what one can do with a moved-from object, see https://stackoverflow.com/a/7028318/576911

      这篇关于为什么C ++移动语义会使源代码结构化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆