1

I'm trying to make a std list that will hold a list of classes that contain a unique pointer. I keep getting an error that says attempting to reference a deleted function.

I understand that the problem is that unique pointers don't have an assignment operator and list is trying to call it, but I can't figure out a way around this.

Here is the code for the class that holds the pointer:

class ptr_hold
{
public:
    ptr_hold(std::list<int>& _lst);
    std::list<int>* get_list();
private:
    std::unique_ptr<std::list<int>> num_list;
};

ptr_hold::ptr_hold(std::list<int>& _lst)
{
    num_list = std::unique_ptr<std::list<int>>(new std::list<int>(_lst));
}

Here is the example code trying to do the assigment:

list<int> lst;
for (int i = 0; i < 5; i++)
{
    lst.push_back(i);
}
ptr_hold ph = ptr_hold(lst);

list<ptr_hold> ph_list;
ph_list.push_back(ph); //error
ph_list.push_back(move(ph)); //error

How do I handle this?

Lucas
  • 567
  • 1
  • 8
  • 21

1 Answers1

2

Well, there is a good chance that you are using VS, since it doesn't automagically generate move member functions for you. (Otherwise ph_list.push_back(move(ph)) would work.)

The unique_ptr does have move-assignment operator, which implies ownership transfer. To call move-assignment operator (or move constructor for that matter) you need to work with rvalue. (T&&, where T is not meant as template here, as that invokes something a bit different) To obtain a rvalue, you either have to work with a temporary (such as a return value from a function) or you have to manually cast lvalue to rvalue. This is done via std::move (to signify intent) or a bit more wordy static_cast.

To solve this, either use emplace_back or declare proper constructors manually, and use either emplace_back or push_back(move()).

---edit---
Note: emplace_back probably shouldn't require move-assignment constructor to work, but you will run into trouble when trying to extract the object from the list.

Xarn
  • 3,460
  • 1
  • 21
  • 43
  • @dyp Noted and changed, thanks. Also I swear I started with the answer before your comment. – Xarn Feb 17 '14 at 00:36
  • Btw you don't need to add "--edit--" or "\[edit\]" when editing parts of your answer, there's a [revision history](http://stackoverflow.com/posts/21818780/revisions), and there's a notifier for others when reading the answer and it just has been edited. – dyp Feb 17 '14 at 00:39
  • @dyp I know, but I feel that it serves a purpose of quickly visually indicating a change that should be noted in the post, or perhaps a later thought-of note/remark. (Or perhaps I am rationalizing a habit from different forums. :-) ) – Xarn Feb 17 '14 at 00:52
  • I tried emplace_back with and without the move function, but that didn't work for me. I'm not familiar with the rvalue, so I'm reading up on that now. – Lucas Feb 17 '14 at 01:11