1
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;
};

I haven't any problems with push_back - just using push_back(std::move(...)).

Also, no problems with:

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

But I also need iterate all elements of container. I doing:

for (auto it = container.begin(); auto it = container.end(); ++it) {
   some_foo(*it); // For instance, another_container_of_same_type.push_back(*it) - to merge 2 containers
}

Or, in special case:

for (auto it = container.begin(); auto it = container.end(); ++it) {
   // some logics
   another_container_of_same_type.push_back(*it); //another container defined same as first one
   // some logics
}

And it not compiles!

I tried:

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

Not compiles. It wants copying constructor nor moving...

Warning: NO, it is not simply merging two containers, I using my custom logics in the merging, also using iterating not for merging only.

Textron
  • 21
  • 2

1 Answers1

0

You didn't show the declaration for some_foo, but likely you didn't take the parameter by reference.

Here's a simple, self-contained example that should get you going.

Live On COliru

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>

// move-only
struct foo {
    foo()           = default;
    foo(foo&&)      = default;
    foo(foo const&) = delete;
    foo& operator=(foo&&)      = default;
    foo& operator=(foo const&) = delete;
};

namespace bmi = boost::multi_index;

using foos = bmi::multi_index_container<foo, bmi::indexed_by<bmi::sequenced<> > >;

void some_foo(foo const&) {}

int main() {
    foos c;

    for (auto& el : c) {
        some_foo(el);
    }
}

Compiles fine.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Note: I included a self-contained example, if you get stuck again later, modify it to post a better question. – sehe May 26 '18 at 22:24
  • I calling another_container.push_back() as the some_foo. (And I mentioned such case in the question) The another_container is defined 100% same as first one. – Textron May 26 '18 at 22:28
  • Now that it has become completely obvious that wish to move the element - always, I'm marking this as duplicate. – sehe May 26 '18 at 23:17
  • https://stackoverflow.com/questions/46082394/move-element-from-boost-multi-index-array - here is no "another_container.push_back()" which is one of my cases here! Your solution isn't applicable for that case because I couldn't rewrite "push_back". – Textron May 26 '18 at 23:47
  • Ok, I asked another question about special push_back case, rathen than "moving" at whole: https://stackoverflow.com/questions/50547949/boost-multi-index-how-to-add-an-element-from-one-container-to-another-if-its-t – Textron May 27 '18 at 00:00