简体   繁体   English

std :: move或std :: forward,参数为std :: unique_ptr <T> &amp;&amp;

[英]std::move or std::forward with parameter std::unique_ptr<T>&&

I have the following template class (stripped down to contain only the relevant parts) with a method called Push written in C++ 11: 我有一个名为Push in C ++ 11的方法,下面的模板类(剥离只包含相关部分):

template<class T, int Capacity>
class CircularStack
{
private:
    std::array<std::unique_ptr<T>, Capacity> _stack;

public:
   void Push(std::unique_ptr<T>&& value)
   {
      //some code omitted that updates an _index member variable

      _stack[_index] = std::move(value);
   }
}

My question is: 我的问题是:

Should I be using std::move or std::forward within Push ? 我应该在Push使用std::move还是std::forward

I am not sure whether std::unique_ptr<T>&& qualifies as a universal reference and therefore should be using forward rather than move . 我不确定std::unique_ptr<T>&&有资格作为通用引用,因此应该使用forward而不是move

I am reasonably new to C++. 我是C ++的新手。

You should use std::move . 你应该使用std::move

std::unique_ptr<T>&& is an rvalue reference, not a forwarding reference*. std::unique_ptr<T>&&是一个右值引用,而不是转发引用*。 Forwarding references in function parameters must look like T&& where T is deduced, ie a template parameter of the function being declared. 在功能参数转发引用必须看起来像T&&哪里T推导,正在申报即函数的模板参数。

* forwarding reference is the preferred name for what you call a universal reference. * 转发引用是您称之为通用引用的首选名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM