Topic: Shouldn't all std functions that require Swappable types indeed call
Author: unknown@this.is.invalid ("Niels Dekker (no reply address)")
Date: Tue, 3 Oct 2006 15:12:25 GMT Raw View
Howard Hinnant wrote:
> Imho, iter_swap(i, j) is nothing more than a convenience function for
> swap(*i, *j). Whether a std::algorithm uses iter_swap or swap should be
> an implementation detail. The important thing from the interface point
> of view is that swap is a customization point in the algorithm. Clients
> can customize what swap is called by providing an implementation which
> will be found via ADL. iter_swap otoh, is not intended to be a
> customization point. I.e. if a std::algorithm calls it, it should call
> it qualified: std::iter_swap(i, j).
Thanks for clarifying this for me. Hope it's clear to other programmers
as well! So a programmer is free to add a function named "iter_swap" to
one of the programmer's namespaces, but this won't have an effect on any
std::algorithm. On the other hand, a programmer should never specialize
std::iter_swap, because it's unspecified whether such specialization
would effect the behaviour of any std::algorithm.
> Any algorithm that requires Swappable, implies only that swap is a
> customization point [and not iter_swap]
So the answer to the question put in the subject line is yes! All std
functions that require Swappable types should indeed have the effect of
calling an appropriate swap function, whenever they're swapping objects
of that type. Which allows the programmer (the client) to customize
each of those std functions. Please correct me if I'm wrong.
Actually none of the two library implementaties I looked at use my Foo
swap function, when I pass Foo pointers to partial_sort,
partial_sort_copy, pop_heap, or sort_heap. But I guess it just proves
that they're not yet C++0x compliant :-)
Kind regards,
Niels Dekker
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: unknown@this.is.invalid ("Niels Dekker (no reply address)")
Date: Mon, 2 Oct 2006 04:58:06 GMT Raw View
Whenever an std function needs to do some swapping, shouldn't this
swapping have the effect of calling a swap function?
According to the Draft (N2009), 17 std functions require some of their
arguments to be iterators that point to a Swappable type:
iter_swap, swap_ranges, reverse, rotate, random_shuffle, partition,
stable_partition, sort, stable_sort, partial_sort, partial_sort_copy,
nth_element, inplace_merge, pop_heap, sort_heap, next_permutation, and
prev_permutation.
The Draft explicitly specifies the effect of iter_swap(a, b) as being
equal to swap(*a, *b) [following the resolution of issue 187, submitted
by Andrew Koenig, C++ Standard Library Defect Report List, Revision
R44].
Also it specifies the effect of std::reverse in terms of applying
iter_swap:
"Effects: For each non-negative integer i <= (last - first )/2,
applies iter_swap to all pairs of iterators first + i, (last - i) - 1."
Now it seems logical to me to specify the effect of the other functions
in terms of applying iter_swap as well. But instead, the effect of
std::swap_ranges is specified in terms of calling swap directly:
"Effects: For each non-negative integer n < (last1 - first1 )
performs: swap(*(first1 + n), *(first2 + n))."
Isn't this an inconsistency? Can't the effect of std::swap_ranges be
described as well by applying iter_swap?
More importantly, for the other 14 std functions it is not mentioned at
all, whether or not either swap or iter_swap is used when sorting,
shuffling, or placing the elements from one position to another. It's
unclear to me whether providing an efficient, non-throwing swap for my
own class does have any effect on the behaviour of calls to these
functions for my class.
Actually, most of those 14 std functions do depend on iter_swap, when
looking at the library implementations of MSVC++ 14.00.50727.42 and GNU
g++ 3.4.4. Only 4 of them don't seem to depend on iter_swap for both
implementations: partial_sort, partial_sort_copy, pop_heap and
sort_heap. Two more of them don't seem to depend on iter_swap on
MSVC++: rotate and stable_partition. (These observations are based on
providing an unimplemented specialization of std::iter_swap<int*, int*>
and looking at the link errors when trying the call each of the 14
functions with int pointers as arguments.)
Shouldn't C++0x specify that whenever one of those 14 std function swaps
some elements pointed to by the iterators passed as its arguments, it
has the effect of applying iter_swap?
Kind regards,
Niels Dekker
www.xs4all.nl/~nd/dekkerware
---
[ 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.comeaucomputing.com/csc/faq.html ]