Opened 14 years ago

Closed 13 years ago

#2243 closed Feature Requests (fixed)

value_initialized should have its own swap functions

Reported by: niels_dekker Owned by: niels_dekker
Milestone: Boost 1.37.0 Component: utility
Version: Boost 1.36.0 Severity: Problem
Keywords: Cc:

Description

For a given type T, the current (1.36.0) version of boost::value_initialized<T> is only Swappable if it is both CopyConstructible and CopyAssignable. IMO, value_initialized<T> should be Swappable whenever T is Swappable. Moreover, when swapping value_initialized<T> objects, the custom swap function of T should be used, whenever available.

I'd like to propose resolving this issue by adding a swap member function to value_initialized<T>, and a non-member swap that calls the member. (As recommended by Scott Meyers, Effective C++ Third Edition, Item 25: Consider support for a non-throwing swap.)

I would like to have value_initialized<T>::swap calling boost::swap to swap its data, using the boost::swap utility by Joseph Gauterin et al, that was recently added to the trunk. The boost::swap utility internally uses argument-dependent lookup (ADL) to find the custom swap function of T, if available. Thereby it works around various compiler version specific bugs regarding ADL, as tested at utility-swap_.html. Moreover, it supports swapping arrays (as requested by ticket #2056), so value_initialized<T>::swap would also support having T as a built-in array type.

Please take a look at the attached patch.

Attachments (1)

swap_value_initialized.patch (3.3 KB ) - added by niels_dekker 14 years ago.

Download all attachments as: .zip

Change History (4)

by niels_dekker, 14 years ago

comment:1 by niels_dekker, 14 years ago

Owner: changed from No-Maintainer to Fernando Cacciola

Assigned to Fernando Cacciola. Fernando, I wouldn't mind committing the patch myself, but only if you think it's okay :-)

comment:2 by niels_dekker, 14 years ago

Owner: changed from Fernando Cacciola to niels_dekker

I've just committed the patch to the trunk: changeset [48424] and [48425]. Please note that before merging this to the release branch, boost/utility/swap.hpp should be added to the release branch.

comment:3 by Daniel James, 13 years ago

Resolution: fixed
Status: newclosed

THis was merged by niels_dekker to release in [49967].

Note: See TracTickets for help on using tickets.