1

The simple code below would not compile using Clang on Mac,

void foo(std::vector<std::unique_ptr<int> > &fromVector, 
         std::multiset<std::unique_ptr<int> > &fromMultiset) {

             std::vector<std::unique_ptr<int> > to;
             auto fv = fromVector.begin();
             to.push_back(std::move(*fv));
             auto fms = fromMultiset.begin();
             to.push_back(std::move(*fms));
         }

Compiler complains that pushing back rvalue obtained by std::move(*fms) necessitates invocation of deleted copy constructor. On the other hand Clang seems to be happy with similar operation on vector. Am I doing something wrong?

LRaiz
  • 667
  • 7
  • 18
  • 1
    `std::set` (and `std::multiset`) need to maintain their invariants. I.e. they need their elements to stay sorted. Moving an element would leave it in a (presumably) *valid, but unspecified* state, which means that it *could* not be at the right place any more. Therefore, both iterator types for sets are *constant iterators*, which prevent you from moving. – dyp Apr 02 '14 at 21:48
  • possible duplicate of [How to change a set element?](http://stackoverflow.com/questions/9600666/how-to-change-a-set-element), related to [what happens when you modify an element of an std::set?](http://stackoverflow.com/questions/908949/what-happens-when-you-modify-an-element-of-an-stdset) and [What is an alternative set-like data structure when modifying elements is necessary?](http://stackoverflow.com/questions/7531608/what-is-an-alternative-set-like-data-structure-when-modifying-elements-is-necess) – dyp Apr 02 '14 at 21:51
  • 2
    Theoretically there is a way to do this, but the committee has yet to accept and provide it: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3645.pdf – Howard Hinnant Apr 02 '14 at 21:56
  • Thanks for responses. I now understand what is going on and will modify my overall design. – LRaiz Apr 03 '14 at 03:36

0 Answers0