std::move 或 std::forward 参数 std::unique_ptr&<T>&



我有一个用C++11编写的名为Push的方法的以下模板类(精简后只包含相关部分):

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);
   }
}

我的问题是:

我应该在Push中使用std::move还是std::forward

我不确定std::unique_ptr<T>&&是否符合通用参考,因此应该使用forward而不是move

我对C++相当陌生。

您应该使用std::move

std::unique_ptr<T>&&是一个右值引用,而不是转发引用*。函数参数中的转发引用必须看起来像T&&,其中推导出T,即被声明函数的模板参数。

*转发引用是您所称的通用引用的首选名称。

相关内容

最新更新