Getting improved iter_swap into 4.0?

Chris Jefferson caj@cs.york.ac.uk
Fri Sep 10 22:48:00 GMT 2004


I attach a patch to stl_algobase.h that calls swap when iter_swap is 
passed two iterators with identical value_type, and also a test case 
that tests a few things. Note that this all tests in this test case pass 
with the existing implementation except (obviously) the single test 
which checks if iter_swap calls swap.

Note that while it might look like this patch changes std::swap, it in 
fact doesn't. I simply had to swap the order of iter_swap and swap so 
that iter_swap could call swap, hence why it appears in the diff.

This is a very simple patch and it wouldn't suprise me if someone else 
can come up with a better method of implementing this. Feel free :)

Now to the important point. I know that offically 4.0 is supposed to be 
frozen now except for a select number of projects. However I personally 
believe that it is very important that this works it's way into 4.0. In 
theory it should not effect existing code at all, and (obviously) 
produces quite huge improvements when people perform operations on large 
objects. I'm sure I'm not the only person who occasionally simply calls 
sort on an array of very long vector<ints>. Note that (as was discussed 
before) it looks like the standard may be changed to require this in a 
later version, and both STLport and icc already use swap to implement 
iter_swap (in fact this implementation is more lax than these, as if the 
value_types of the iterators are different it falls back on a simple 
assignment swap, which is not the case in icc and STLport).

However, it is also possible that it may break some code which was badly 
written before. The two main areas where I believe this can happen is 
where the value_type of the iterators have not been correctly defined or 
(obviously) where a swap(T&,T&) function has been declared but 
incorrectly implemented. I feel users will be "happier" with a change 
which 'breaks' code (even if it was originally incorrectly implemented) 
at a .0 release more than any other.

Of course if anyone feels that there might be as-yet unknown bugs to 
sort out from this, then I'm happy to leave it. However what are the 
chances of sneaking it into 4.0?

Sorry for the long rambling message,

Chris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch_stl_algobase.h
Type: text/x-chdr
Size: 2510 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20040910/95438e24/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: iter_swap.cc
Type: text/x-c++src
Size: 2917 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20040910/95438e24/attachment-0001.bin>


More information about the Libstdc++ mailing list