• entries
101
122
• views
58386

# ...

438 views

EDIT: since I originally wrote this the MSDN page has been updated to mention that its behavior does not conform to the C++ standard.

Here's a question, will this code compile? (assume someSet is a std::set, and that 512 is a key that exists in it)

Quote:
 std::set::iterator foo = someSet.erase(512);

This comes from a conversation on #gamedev where it worked with visual studio but was choking on gcc.

Initially I suggested that std::set might not have any erase members that returned anything - though that was wrong as it appears that there is almost definitely one that returns a size_type).

The consensus was that since MSDN's page suggests it returns an iterator or size_type, that is the standard behavior. The belief was that the erase functions listed at the SGI page for std::set, none of which return an iterator, was not standard. A further link to the GNU libstdc++ docs resulted in suspicion that gcc might be in the wrong here - and this was from people whose programming abilities I highly respect.

The beautiful thing is that since I don't have a copy of the standard nearby I have no idea which is correct. Google gave me this mailing list thread which suggests that the STL version of set had an erase member which returned an iterator, but standard C++ doesn't. Also, the freely available draft standard suggests in section 23.3.3 that it doesn't return an iterator.

Until I have a copy of the standard in front of me I can either trust what MSDN and some of the more competent members of gamedev have to say, or I can hope that some page I found on google and a draft version of the standard is correct, yuck.

Admittedly this might not seem like a big deal, and if you were only using Visual Studio as a build environment it wouldn't be a problem - but personally I think its symptomatic of some of the larger problems with C++.

## 1 Comment

Here you go, from ISO/IEC 14882:2003:
[23.1.2.7] (Associative container requirements)
a.erase(k)   size_type    erases all the elements in the container   log(size()) +
with key equivalent to k.                  count(k)
returns the number of erased elements.

a.erase(q)        void    erases all the elements pointed to by q.   amortized constant

a.erase(q1,q2)    void    erases all the elements in the range       log(size())+ N
[q1, q2)                                   where N is the distance
from q1 to q2
[23.3.3.2] (Class template set)
void erase(iterator position);
size_type erase(const key_type& x);
void erase(iterator first, iterator last);

## Create an account

Register a new account