Why doesn't iter_swap use swap?

Martin Sebor sebor@roguewave.com
Mon Aug 23 21:40:00 GMT 2004


chris wrote:

> Hello self :)
> 
> Just to pad out my earlier mail, it looks like intel's implementation 
> does use swap to implement iter_swap, and stlport's implementation 
> requires the two parameters to iter_swap must have the same type (unlike 
> the standard).
> 
> There is also a defect report against iter_swap ( 
> http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html , defect 
> 187 ), which discusses how to be honest it's a bit of a mess.
> 
> I see this has been previously posted about but wasn't discussed in 
> depth. However for performing STL algorithms in constructs of any kind 
> of complexity I imagine this is causing some serious dents in performance.
> 
> One reason discussed in the standard about why not to use swap() is that 
> iter_swap may work in some places where swap() doesn't (vector<bool> is 
> mentioned). I'm not an expert on this, but we seem to in general require 
> "normal assignment" in for example __unguarded_linear_insert, and other 
> places.

The "new" iter_swap will also not work in cases where the two
iterators' value_types are distinct types:

     int i;
     double d;
     std::iter_swap (&i, &d);

I suppose iter_swap may not have been intended to work with such
iterators but I don't see any requirement that the two types be
the same, so the resolution breaks code written to exploit this
capability. (Our implementation of iter_swap allows heterogeneous
types.)

I would like to see the standard changed to either explicitly
require that iter_swap's iterators' value_types be identical
or the resolution of issue 187 reverted/changed so as not to
force this new requirement.

Martin



More information about the Libstdc++ mailing list