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

kei*_*ith 7 c++ c++11

我有一个名为PushC++ 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);
   }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:

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

我不确定是否std::unique_ptr<T>&&有资格作为通用参考,因此应该使用forward而不是move.

我是C++的新手.

Tar*_*ama 9

你应该用std::move.

std::unique_ptr<T>&&是一个右值引用,而不是转发引用*.转发在功能参数引用必须像T&&这里T推导,即函数的模板参数被声明.

*转发引用是您称之为通用引用的首选名称.