[英]std::move or std::forward with parameter std::unique_ptr<T>&&
我有一个名为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);
}
}
我的问题是:
我应该在
Push
使用std::move
还是std::forward
?
我不确定std::unique_ptr<T>&&
有资格作为通用引用,因此应该使用forward
而不是move
。
我是C ++的新手。
你应该使用std::move
。
std::unique_ptr<T>&&
是一个右值引用,而不是转发引用*。 在功能参数转发引用必须看起来像T&&
哪里T
推导,正在申报即函数的模板参数。
* 转发引用是您称之为通用引用的首选名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.