-2

I've run into a problem with trying to move unique_ptrs between containers. I have a std::unordered_set called elements that contains a bunch of unique_ptrs. I want to move some of them to another unordered_set called subelements. How do I do this?

Here's my function:

void MeshContainer::MoveSubelements(){

  int mesh_dim = MeshDimension();

  for(auto el=elements.begin(); el!= elements.end(); ++el){
    if((*el)->getDim() != mesh_dim){
      subelements.insert(std::move(*el));
      elements.erase(*el);
    }
  }

}

I get the following compiler error using the above code:

error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = MEl; _Dp = std::default_delete]’

I think the issue has to be with the copy constructor, but I thought that using std::move would resolve this.

Any ideas?

Thanks!

  • What type is the `unique_ptr` containing? – yizzlez Jun 11 '14 at 23:25
  • 1
    Which line is the error flagging? – aschepler Jun 11 '14 at 23:26
  • 3
    This will not work, unfortunately. The elements of `std::set` are immutable (= the iterators are constant iterators because one of the class invariants is that the set is sorted). There's a proposal to add splicing of trees to the tree-based containers IIRC, to support such things. Edit: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3586.pdf – dyp Jun 11 '14 at 23:27
  • Unique_ptr is templated on an abstract class called MEl. the set is of course generated with concrete classes of uniuq_ptr's. – user3335011 Jun 11 '14 at 23:29
  • dyp: is flags the 'subelements.insert(std::move(*el))' line. – user3335011 Jun 11 '14 at 23:29
  • 1
    Related/duplicate: http://stackoverflow.com/q/14429167/420683 – dyp Jun 11 '14 at 23:30

1 Answers1

0

std::set and std::multiset only provide const access to their elements. This means you cannot move something out of the set. If you could move items out (or modify them at all), you could break the set by changing the sort order of the items. So C++11 forbids it.

So your attempt to use the std::move algorithm will just invoke the copy constructor.

Once you place unique_ptrs in a set, they're stuck.

source and thank you dyp.

Community
  • 1
  • 1
Sophit
  • 491
  • 2
  • 8
  • OP's not using the [`std::move` algorithm](http://en.cppreference.com/w/cpp/algorithm/move) but the [`std::move` function](http://en.cppreference.com/w/cpp/utility/move) (which is badly named, since it's effectively a `static_cast(..)` so it does perform a move). – dyp Jun 11 '14 at 23:43
  • How do you figure? I thought I was using std::move defined in . Either way, I'm still stuck, correct? – user3335011 Jun 11 '14 at 23:47
  • 4
    -1 for blatantly copying an answer to a duplicate question. http://stackoverflow.com/a/14429376/440119 – Benjamin Lindley Jun 11 '14 at 23:47
  • 1
    Blatantly copying *with a link to the original*. What exactly is the problem? – Sophit Jun 12 '14 at 22:49