Your problem is that you have pushed temporaries into the list:
for (auto ele : vec) {
list.push_back(ele);
}
You probably meant to write:
for (auto& ele : vec) {
list.push_back(ele);
}
This is classic confusion when starting to work with intrusive containers: nothing is by value like all the with all the standard library containers.
To avoid situations like this, consider using the auto-unlinking hook mode.
Demo
Even safer than having to think about reference-qualifying the loop variable, is having no loop at all:
boost::intrusive::list<X> list(vec.begin(), vec.end());
Live On Coliru
#include <iostream>
#include <iterator>
#include <vector>
#include <boost/intrusive/list.hpp>
struct X : public boost::intrusive::list_base_hook<> {
X(int a_in) : a{a_in} {}
int a;
friend std::ostream& operator<<(std::ostream& os, X const& x) { return os << "{" << x.a << "}"; }
};
int main() {
std::ostream_iterator<X> out(std::cout << std::unitbuf, " ");
std::vector<X> vec { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
boost::intrusive::list<X> list(vec.begin(), vec.end());
std::cout << "before: "; std::copy(list.begin(), list.end(), out);
list.remove_if([](X const& x) { return 0 == (x.a % 2); });
std::cout << "\nafter: "; std::copy(list.begin(), list.end(), out);
}
Prints
before: {1} {2} {3} {4} {5} {6} {7} {8} {9} {10}
after: {1} {3} {5} {7} {9}
With auto_unlink
:
struct X : public bi::list_base_hook<bi::link_mode<bi::auto_unlink> > {
Note, you need to disable constant-time size()
support for the list<>
(see reference)
With that in place, even adding
vec.erase(vec.begin()+4);
will correctly unlink the corresponding node from the intrusive list:
Live On Coliru
#include <iostream>
#include <iterator>
#include <vector>
#include <boost/intrusive/list.hpp>
namespace bi = boost::intrusive;
struct X : public bi::list_base_hook<bi::link_mode<bi::auto_unlink> > {
X(int a_in) : a{a_in} {}
int a;
friend std::ostream& operator<<(std::ostream& os, X const& x) { return os << "{" << x.a << "}"; }
};
int main() {
std::ostream_iterator<X> out(std::cout << std::unitbuf, " ");
std::vector<X> vec { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
bi::list<X, bi::constant_time_size<false> > list(vec.begin(), vec.end());
std::cout << "before: "; std::copy(list.begin(), list.end(), out);
list.remove_if([](X const& x) { return 0 == (x.a % 2); });
std::cout << "\nafter: "; std::copy(list.begin(), list.end(), out);
vec.erase(vec.begin()+4);
std::cout << "\nauto-unlinked: "; std::copy(list.begin(), list.end(), out);
}
Prints
before: {1} {2} {3} {4} {5} {6} {7} {8} {9} {10}
after: {1} {3} {5} {7} {9}
auto-unlinked: {1} {3} {6} {8} {10}