[英]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.