Thursday 11 November 2010

Sneaky use of sequence points in c++

i've found a very interesting corner case where fiddly knowledge of
sequence points and STL are used.
apparently it's not possible to use remove_if on associate arrays but
this didn't stop me trying for a good 30 mins before i discovered this
workaround:

for(; iter != endIter; )
{
if(Some Condition)
{
// is it safe ?
aMap.erase(iter++);
}
else
{
++iter;
}
}


There's no evidence in the answer, but it's apparent after digging
through the numerous overloads and associated nastiness that its
apparently excusable simplicity comes down to sneaky use of sequence
points.

http://www.cplusplus.com/reference/algorithm/remove_if/
http://stackoverflow.com/questions/800955/remove-if-equivalent-for-stdmap/800984#800984

1 comment:

Anonymous said...

This is common and used within libstdc++.