Boost logo

Boost :

From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2019-09-08 12:21:08


Dear Experts,

Is it possible to make intrusive container elements movable?

Here's the scenario. Say I have

struct Thing { int id, string name };

and I want a container of those indexed by both the id and the
name. Using Boost.Intrusive I can add two hooks to the struct
and have two intrusive::sets, one using id as the key and the other
using name. All good.

But say my ids are a contiguous range of integers starting at
zero. It would be much better to store the Things in a vector
(or similar), indexed by the id, and use Intrusive only for the
name index.

Or if my ids are not contiguous, I might still prefer to keep
the things in a vector sorted by id and do id lookup by binary
search (i.e. a flat_set).

In both cases, the issue is that when the vector expands or is
sorted the elements are moved, and this will invalidate the
pointers used for the name index.

Is is possible to fix this using move constructors and move
assignment for the hooks? I.e. those operations could adjust
the pointers pointing to the element's old location to point to
its new location. (This has some similarities to how auto-unlink
hooks already fix pointers to remove the being-deleted item.)

Maybe there is some complication that makes this impossible.
Or maybe this is already possible??

Any thoughts anyone?

Regards, Phil.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk