Boost logo

Boost :

From: Niels Dekker - mail address until 2008-12-31 (nd_mail_address_valid_until_2008-12-31_at_[hidden])
Date: 2008-06-21 11:54:11

>> Would it be okay to you if we would add support for array types to
>> the Boost.Swap utility that is located in the sandbox?

David Abrahams wrote:
> Whether or not this is a good idea depends on whether you think swap
> is fundamentally an O(1) and/or nonthrowing operation.

The array support I would like to add to boost::swap is non-throwing, as
long as the array element type has a non-throwing swap. Because it would
do an elementwise swap of its two (array) arguments. The addition is
needed to implement swap functions for wrappers like value_initialized.

Suppose you have two value_initialized arrays of vectors:
  value_initialized<std::vector<int>[42]> v1, v2;
Swapping such value_initialized objects is currently quite expensive
(and throwing), doing three std::vector copy operations for each of the
42 array elements, even when using boost::swap (from the sandbox).
Basically that's just because value_initialized doesn't yet have a
custom swap function. Which could typically be implemented as follows:

  namespace boost {
    template <typename T> void swap(
      value_initialized<T>& lhs,
      value_initialized<T>& rhs)
      // Assuming that sandbox/swap is available:
      boost::swap( get(lhs), get(rhs) );

But such an overload would need boost::swap to support arrays! Otherwise
it simply wouldn't compile, when trying to swap
value_initialized<std::vector<int>[42]> objects.

What do you think?
See also (now
having status Ready).

Kind regards,


Boost list run by bdawes at, gregod at, cpdaniel at, john at