Why doesn't std::set::erase take a const_iterator?

I believe that this is/was a defect. In a more current draft (N3242) of the upcoming standard, set::erase takes a const_iterator.

I believe that this is/was a defect. In a more current draft (N3242) of the upcoming standard, set::erase takes a const_iterator: iterator erase(const_iterator position); This paper from 2007 illustrated that error and showed implementations to avoid it. I am not sure if this paper is the reason for the change in the standard, but it's probably a good guess.

Thank you very much. It's a great paper and explains things thoroughly. – Armen Tsirunyan Jul 13 at 20:30 kerrek-sb already said that 25 minutes ago in a comment.

– evnu Jul 13 at 20:32 he said it's fixed in N3242, but didn't present the paper with description of the issue with the rationale and possible alternatives... did he? – Armen Tsirunyan Jul 13 at 20:34 @armen-tsirunyan: no. I only wanted to give him credit for being faster.

– evnu Jul 13 at 20:37 Yea, apparently it was this paper and option B. – Tomalak Geret'kal Jul 13 at 21:23.

Can't really think of any reason for it to need an iterator, so I'm leaning toward the arbitrary: any op that modifies the structure would take an iterator to let the user know that what previously worked with the iterator might not afterward: erase invalidates the iterator. Insert(iter, val) changes the next value. Etc.

Yes, well, that's why erase isn't a const function, because it modifies the set. But it doesn't and can't modify *it... I didn't understand your point, sorry. What consistency are you talking about?

Consistency with what? – Armen Tsirunyan Jul 13 at 20:17 I should not have used that word, sorry. Tired.

I meant only to say that, while erase modifies the set, it also modifies the behavior of that specific iterator. It's not too far of a leap to believe someone originally made it an iterator to signal that, despite it serving no real purpose. – Cory Nelson Jul 13 at 20:23 Seems a bit far fetched because a const_iterator and iterator have exactly the same rules of being invalidated.

But then, who knows? :) – Armen Tsirunyan Jul 13 at 20:27.

My only guess is because insert, upper_bound, lower_bound and find are returning iterator (not const iterator). I don't see other explanation.

5 An iterator can be converted to a const_iterator trivially, so I don't think that's an impediment. – Mark Ransom Jul 13 at 20:02 @Mark Well, other explanation could be : a bug in requirements. – VJovic Jul 13 at 20:07 @VJo: "a bug in requirements" could well be an explanation, but your theory about insert, find, etc.Just couldn't – Armen Tsirunyan Jul 13 at 20:08 1 Judging by the paper linked by in the accepted answer, this theory is spot on.

It isn't a reasonable explanation, but who said the standards comittee was reasonable? – Dennis Zickefoose Jul 13 at 21:28 @Mark @Armen It is true that the iterator could be trivially converted into a const_iterator, but it would still require conversion, and the STL was designed to be as optimal as possible (for example, there are no boundary check when using vector::operator) – VJovic Jul 137 at 6:59.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions