Boost logo

Boost Users :

From: Pavel Vazharov (freakpv_at_[hidden])
Date: 2022-11-01 15:44:11


Hi there,

Is there a way to emplace (key, value) in boost multi_index hashed index in
such a way that the value is not moved/stolen if the key is already present?
As far as I checked the implementation for emplace (and emplace_) the node
is eagerly created and the key and the value are moved into the node and
then the insertion operation takes place. If the insertion fails the node
is destroyed but the outside value is lost/stolen in all cases.
What I actually need is to create the value only if the key is not present,
because the creation is expensive, and I'd like to avoid the find+emplace
or insert empty value + modify call. (As far as I checked, the value can't
be modified directly from the returned iterator because it's const and will
need const_cast to work).
I thought to use emplace with some machinery for lazy creation
struct creator
{
operator value_type () { return ...; } // create the value lazily, on demand
};
The creator is passed to emplace instead of the actual value. However, this
doesn't help because the node is eagerly created and the above operator is
always called.

Thanks,
Pavel.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net