Topic: why two swappers: iter_swap & swap
Author: "Anthony Williams"<anthwil@nortelnetworks.com>
Date: Wed, 23 Jan 2002 10:43:44 CST Raw View
"Oliver Schoenborn" <oliver.schoenborn@utoronto.ca> wrote in message
news:z5638.2406$Ii5.1381738@news20.bellglobal.com...
> Hello, does anyone know why there are two swap functions defined in the
STL,
> std::swap and std::iter_swap? The problem is, that if as a library user
I
> specialize std::swap for one of my classes, I certainly expect every
> swap-like operation to use that specialization. If I either forget to
> create a separate specialization for std::iter_swap or, worse, don't know
> about it, I'm in trouble.
>
> Unfortunately most STL algorithms use std::iter_swap (in the SGI version
> anyways), e.g. std::sort. Logically, I expect std::sort to use swapping
> operations when sorting my container. Yet, it doesn't use std::swap, but
> something else that IMHO most STL users would not know about... Is it
> unreasonable to expect that all swap operations will make use of the
user's
> specializations?
Most STL algorithms operate on iterator ranges. It therefore makes sense to
use iter_swap when swapping elements within those ranges, since that is its
intended purpose --- swapping the elements pointed to by two iterators. This
allows optimizations for node-based sequences such as std::list, whereby the
nodes are just relinked, rather than the data actually being swapped. For
the general case, I would expect std::iter_swap(a,b) to do std::swap(*a,*b),
however this is not guaranteed. It is not specified whether any of the
standard algorithms use std::iter_swap or std::swap, or something else.
You only need to specialize iter_swap if you are designing iterators, and
know that it is easy to swap the pointed-to elements without actually moving
the data.
Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optical Components Ltd
The opinions expressed in this message are not necessarily those of my
employer
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Oliver Schoenborn" <oliver.schoenborn@utoronto.ca>
Date: Tue, 22 Jan 2002 15:24:45 GMT Raw View
Hello, does anyone know why there are two swap functions defined in the STL,
std::swap and std::iter_swap? The problem is, that if as a library user I
specialize std::swap for one of my classes, I certainly expect every
swap-like operation to use that specialization. If I either forget to
create a separate specialization for std::iter_swap or, worse, don't know
about it, I'm in trouble.
Unfortunately most STL algorithms use std::iter_swap (in the SGI version
anyways), e.g. std::sort. Logically, I expect std::sort to use swapping
operations when sorting my container. Yet, it doesn't use std::swap, but
something else that IMHO most STL users would not know about... Is it
unreasonable to expect that all swap operations will make use of the user's
specializations?
I'd appreciate hearing from any of you who might know if this was a
conscious decision in 1997 or an oversight or a "good idea at the time". I
have looked in the issues list and nothing discusses this important issue,
but perhaps it's been discussed somewhere else? If it seems useful, I'd be
willing to pursue this further with the LWG.
Thanks for any feedback,
Oliver Schoenborn
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]