$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
From: Niels Dekker - mail address until 2008-12-31 (nd_mail_address_valid_until_2008-12-31_at_[hidden])
Date: 2008-06-24 12:20:45
David Abrahams wrote:
> I haven't formed an opinion yet, but the fact remains that swapping a
> boost::array would have very different efficiency characteristics from
> swapping the analogous std::vector, so we ought to think it through
> carefully.
Howard Hinnant wrote:
> If it helps, here is a test program using gcc 4.0.1:
...
> template <class T, std::size_t N>
> void
> swap1(std::tr1::array<T, N>& a, std::tr1::array<T, N>& b)
> {
> std::tr1::array<T, N> temp(a);
> a = b;
> b = temp;
> }
>
> template <class T, std::size_t N>
> void
> swap2(std::tr1::array<T, N>& a, std::tr1::array<T, N>& b)
> {
> std::swap_ranges(a.begin(), a.end(), b.begin());
> }
...
> On my machine:
>
> $ g++ -O3 test.cpp
> $ ./a.out
> swap1 time is 0.172796 seconds
> swap2 time is 1.3e-05 seconds
Cool! Thanks, Howard. The swap member function of boost::array also
calls std::swap_ranges, like your swap2:
// swap (note: linear complexity)
void swap (array<T,N>& y) {
std::swap_ranges(begin(),end(),y.begin());
}
Unfortunately std::swap_ranges doesn't always pick the custom swap
function of T. At least, the STL implementation that comes with MSVC
2008 has swap_ranges calling std::iter_swap, which calls std::swap,
without doing argument-dependent lookup. :-( Luckily, once boost::swap
would be in the trunk, boost::array::swap could be implemented simply by
swapping its one and only data member, elems (a built-in array of T):
void swap (array<T,N>& y) {
// Assuming that boost::swap supports built-in arrays:
boost::swap(this->elems, y.elems);
}
Having boost::array::swap call boost::swap would have two advantages: it
would support a boost::array containing built-in arrays as elements
(boost::array<T[M],N>), and it would use ADL to pick the custom swap
function of T.
What do you think?
Kind regards, Niels