In my project I need container that holds smart pointers to an data unit instances. I write the class (simple example):
template <typename T>
class Queue
{
public:
void push(const T & param)
{
m_deque.push_front(param);
}
private:
std::deque<T> m_deque;
};
Than I want to push the one:
int main()
{
Queue< std::unique_ptr<DataBox> > queue;
std::unique_ptr<DataBox> d1(new DataBox(11));
queue.push(d1);
return 0;
}
And VS2017 compiler says that I can't do this:
Error C2280 std::unique_ptr<DataBox,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
As I understand the reason of the error is attempt to make copy of unique_ptr. But if I change signature to:
void push(T && param) {...}
and the function call
queue.push( std::move(d1) );
I have this error again. So the question is - how should I implement push()
that will move unique_ptr
to the queue?