403. basic_string::swap should not throw exceptions

Section: [string.swap] Status: CD1 Submitter: Beman Dawes Opened: 2003-03-25 Last modified: 2016-11-12

Priority: Not Prioritized

View all other issues in [string.swap].

View all issues with CD1 status.


std::basic_string, 24.3.2 [basic.string] paragraph 2 says that basic_string "conforms to the requirements of a Sequence, as specified in (23.1.1)." The sequence requirements specified in (23.1.1) to not include any prohibition on swap members throwing exceptions.

Section 26.2 [container.requirements] paragraph 10 does limit conditions under which exceptions may be thrown, but applies only to "all container types defined in this clause" and so excludes basic_string::swap because it is defined elsewhere.

Eric Niebler points out that 24.3.2 [basic.string] paragraph 5 explicitly permits basic_string::swap to invalidates iterators, which is disallowed by 26.2 [container.requirements] paragraph 10. Thus the standard would be contradictory if it were read or extended to read as having basic_string meet 26.2 [container.requirements] paragraph 10 requirements.

Yet several LWG members have expressed the belief that the original intent was that basic_string::swap should not throw exceptions as specified by 26.2 [container.requirements] paragraph 10, and that the standard is unclear on this issue. The complexity of basic_string::swap is specified as "constant time", indicating the intent was to avoid copying (which could cause a bad_alloc or other exception). An important use of swap is to ensure that exceptions are not thrown in exception-safe code.

Note: There remains long standing concern over whether or not it is possible to reasonably meet the 26.2 [container.requirements] paragraph 10 swap requirements when allocators are unequal. The specification of basic_string::swap exception requirements is in no way intended to address, prejudice, or otherwise impact that concern.

Proposed resolution:

In [string.swap], add a throws clause:

Throws: Shall not throw exceptions.