Boost logo

Boost Users :

From: joaquin_at_[hidden]
Date: 2008-04-21 13:00:22


Kowalke Oliver (QD IT PA AS) escribió:
> Hi,
> how can I ensure that an iterator is still valid (item was not
> removed from the index)?
>

Well, there is no legal way to determine whether an iterator is valid or
not unless you already
have this information from other sources (the state your program is in
or other contextual info).
The reason is that if you've got an invalid iterator you cannot do
anything with it except assign
it a new valid value:

iterator it=cont.find(...);
cont.erase(it); // "it" is now invalid
*it; // ILLEGAL
++it; --it; // ILLEGAL
it2=it; // ILLEGAL
foo(it); // ILLEGAL, provided foo accepts "it" by value
it=cont.find(...); // OK

[That said, you can grossly abuse the functionality provided by
Boost.MultiIndex's safe mode
(http://www.boost.org/doc/libs/1_35_0/libs/multi_index/doc/tutorial/debug.html#safe_mode
)
to check whether an iterator is valid, but this is totally
non-recommendable as safe mode is a
debugging aid and not program-level functionality, you really oughtn’t
do it.]

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


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