Std :: move or std :: forward with the parameter std :: unique_ptr <T> &&

I have the following template class (stripped down to contain only the relevant parts) using the Push method written 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 use std::move or std::forward inside Push ?

I am not sure that std::unique_ptr<T>&& qualifies as a universal reference and therefore should use forward , not move .

I am new to C ++.

+7
c ++ c ++ 11
source share
1 answer

You should use std::move .

std::unique_ptr<T>&& is a rvalue reference, not a forwarding reference *. Forwarding links in functional parameters should look like this: T&& where T is displayed, i.e. Template parameter of the declared function.

* Forward link is the preferred name for what you call a universal link.

+9
source share

All Articles