
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 www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#809 (now having status Ready). Kind regards, Niels