I have to say that I think Sid is getting a bad rap.
He posted a legitimate question and the first answer from kbw was clearly wrong in a few respects as m4ster r0shi pointed out.
Sid then graciously ignored kbw's inaccurate comments and re-iterated his original question.
To which kbw said the code was "clearly wrong".
Sid, then stated that his code was "not wrong" and for everyone's information he posted the correct answer to the problem.
At this point we simply have two people with a difference of opinion about the "wrong" status of the original code.
Where this conversation went over the edge, I believe, is where Duoas made an ill judged observation that I do not believe was particularly fair. And, in fact, I think it was quite rude.
Duoas wrote: |
---|
Woah, you are an arrogant snot, aren't ya? |
As Albatross said, that didn't help the conversation at all.
But as to whether the original code was "wrong" or not I would say this. It rather depends on what you call "wrong".
Sid is right in that the answer to this question is indeed quite interesting.
Sid wrote: |
---|
The correct answer ( I got it on bytes and it is quite interesting ). |
I have looked at the C++ standard and although I may have missed something I can not see anywhere that it is stipulated that all iterators to a std::set must be const iterators.
So in this respect it is not "wrong" according to the standard to use iterators that can modify the elements of a std::set.
However doing so is, in my opinion, compromises the abstraction. A std::set is both ordered and unique. So allowing an external reference to modify an individual element exposes the std::set to the danger of having its abstraction corrupted. The reference could be used to modify the set to become unordered or to contain duplicate values.
It seems that library designers have plugged this "flaw" and now all iterators from a std::set are const iterators.
This obviously breaks some code.
Was the previous code "wrong"?
I am sure that could be argued both ways. But it was (as far as I can tell) in compliance with the standard.