I don't think you can use swap()
here - that's for swapping containers, not nodes within containers. You can remove the elements and re-insert them at the swapped locations, though:
void swapItems(boost::intrusive::list<MyClass> &list, boost::intrusive::list<MyClass>::iterator it1, boost::intrusive::list<MyClass>::iterator it2)
{
if (it1 == it2) return;
MyClass &node1 = *it1;
MyClass &node2 = *it2;
it1 = list.erase(it1);
if (it1 == it2) { //1 was immediately followed by 2
list.insert(++it2, node1);
return;
}
it2 = list.erase(it2);
list.insert(it2, node1);
list.insert(it1, *node2);
}
Of course, writing this function as tailored to MyClass
is almost absurd - it would be much better served as a function template to work with an arbitrary class. It could perhaps even templated by container type, but you'd have to be more careful there with regard to iterator invalidation.
An alternative would be to provide something like a swapValues()
function in your class anc call that:
void MyClass:swapValues(MyClass &other)
{
using std::swap;
swap(nValue, other.nvalue);
//dtto for other members, but NOT for the boost::intrusive linking structure
}