c++ - Assigning a new value to a iterator of a intrusive container -
while working boost intrusive container splay_set, have reset local iterator member variables. please see sample code below -
#include <boost/intrusive/splay_set.hpp> using namespace boost::intrusive; class obj { public: obj(){}; ~obj(){}; boost::intrusive::list_member_hook<boost::intrusive::link_mode<boost::intrusive::normal_link> > m_memberhook; private: int a; }; typedef splay_set<obj, compare<greater<obj> >, member_hook<obj, splay_set_member_hook<boost::intrusive::link_mode<boost::intrusive::normal_link> >, &obj::m_memberhook> > storagesset; typedef storagesset::iterator storagessetiter; class storage { public: bool init(storagesset& sset) { // error: "no match operator= in ..." m_curiter = sset.begin(); ////<<<<------------- how set new iterator m_enditer = sset.end(); ////<<<<------------- how set new iterator } protected: storagessetiter m_curiter; storagessetiter m_enditer; };
intrusive container doesn't support assignment suppose. cannot initialize splay_set
iterator via member initializer list. there limited example on boost , other site. none gives clear answer question.
my question should assign new value splay_set iterator (and in general sense intrusive container.)
the reason why const_iterator
s because modifying fields may break container invariants.
e.g. when building set across elements of struct { int key; std::string value; };
, modifying key
field through iterator lead [undefined behaviour]
the clean way update things first remove, reinsert modified item.
if hurts performance badly
- reconsider design (consider
mutable
members non-key fields, consider including non-key data reference) - punch hole in abstraction using
const_cast<>
cast away const. danger don't pass reference other party, because break container invariants since might not know leave key fields alone
Comments
Post a Comment