使用std :: move c ++ 11后持续多长时间 [英] What lasts after using std::move c++11

查看:185
本文介绍了使用std :: move c ++ 11后持续多长时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在变量中使用std :: move后,可能是类中的字段:

After using std::move in a variable that might be a field in a class like:

class A {
  public:
    vector<string>&& stealVector() {
      return std::move(myVector);
    }
    void recreateMyVector() {
    }
  private:
    vector<string> myVector;        
};

我如何重新创建向量,就像一个清晰的向量?在std :: move后的myVector中剩下什么?

How would I recreate the vector, like a clear one? What is left in myVector after the std::move?

推荐答案

常见的咒语是一个变量,从处于有效但未指定的状态。这意味着可以销毁分配给该变量,但不能使用任何其他变量。

The common mantra is that a variable that has been "moved-from" is in a valid, but unspecified state. That means that it is possible to destroy and to assign to the variable, but nothing else.

a href =http://www.elementsofprogramming.com/book.html> Stepanov 称之为部分形成,我相信这是一个不错的术语。)

(Stepanov calls this "partially formed", I believe, which is a nice term.)

要清楚,这不是一个严格的规则;相反,它是如何思考移动的一个指南:在你从一个东西后,你不应该再使用原始的对象了。

To be clear, this isn't a strict rule; rather, it is a guideline on how to think about moving: After you move from something, you shouldn't want to use the original object any more. Any attempt to do something non-trivial with the original object (other than assigning to it or destroying it) should be carefully thought about and justified.

但是,在每个特定的对象中case,可能有一些额外的操作,对移动对象有意义,你可能想利用这些。例如:

However, in each particular case, there may be additional operations that make sense on a moved-from object, and it's possible that you may want to take advantage of those. For example:


  • 标准库容器描述其操作的前提条件;操作没有pre­条件都很好。唯一有用的是 clear(),也许 swap()交换)。还有其他没有前提条件的操作,例如 size(),但是根据上述推理,你不应该在任何

  • The standard library containers describe preconditions for their operations; operations with no pre­conditions are fine. The only useful ones that come to mind are clear(), and perhaps swap() (but prefer assignment rather than swapping). There are other operations without preconditions, such as size(), but following the above reasoning, you shouldn't have any business inquiring after the size of an object which you just said you didn't want any more.

unique_ptr< T,D> 确保在移出之后,它为空,您可以在有条件地拥有所有权的情况下利用它:

The unique_ptr<T, D> guarantees that after being moved-from, it is null, which you can exploit in a situation where ownership is taken conditionally:

std::unique_ptr<T> resource(new T);
std::vector<std::function<int(std::unique_ptr<T> &)> handlers = /* ... */;

for (auto const & f : handlers)
{
    int result = f(resource);
    if (!resource) { return result; }
}

处理程序如下所示:

int foo_handler(std::unique_ptr<T> & p)
{ 
    if (some_condition))
    {
        another_container.remember(std::move(p));
        return another_container.state();
    }
    return 0;
}

一般来说,让处理程序返回一些其他类型的状态那个indi­ cates是否从唯一的指针,所有权,但由于标准实际上guaran­ tees,从一个唯一的指针移动,将其留作为null,我们可以利用它来传递这个信息在唯一的指针本身。

It would have been possible generically to have the handler return some other kind of state that indi­cates whether it took ownership from the unique pointer, but since the standard actually guaran­tees that moving-from a unique pointer leaves it as null, we can exploit that to transmit that information in the unique pointer itself.

这篇关于使用std :: move c ++ 11后持续多长时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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