And how realistic is the use of such a thing here:

typedef boost::multi_index_container<foo, ... struct foo { ... std::unique_ptr<Bar> bar; // requires move-semantic only foo() { bar = std::make_unique<Bar>(); } foo(foo&&) = default; }; 

push_back compiles successfully - just push_back (std :: move (...)).

The search is also compiled if I do not iterate, but immediately pass the iterator obtained from find to the function:

 auto it = container.get<...>().find(...); container.erase(it); 

But I still need to go through all the elements of the container. I do this:

 for (auto it = container.begin(); auto it = container.end(); ++it) { some_foo(*it); // Например, another_container.push_back(*it) - чтобы добавить элемент в другой контейнер } 

And it does not compile. None of the options:

 some_foo(std::move(*it)) some_foo(std::move(it)) some_foo(it) some_foo(*it) 

It wants a copy constructor instead of a constructor move ...

  • Do you want to move the item before removing it from the container? - Cerbo
  • @Cerbo put in another container, and whether the removal will not matter. - Textron
  • After the move must be deleted. - Cerbo
  • @Cerbo Ok. Although so. And then no code is not working. - Textron

1 answer 1

Suppose that your code has earned and the system has “eaten” and moved the container element to a function, what will happen to the container? Relocation is a modifying operation (in general), and in multindex it uses information from nodes to build a tree.

Yes, even if I didn’t use it and it would be an ordinary vector, you cannot just take an element in an incomprehensible state in the container, unless of course this container is not immediately removed. Just find the item you need, remove it from the container (remove), and then move it.