This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++11 status.

### 1349. `swap` should not throw

Section: 16 [library] Status: C++11 Submitter: BSI Opened: 2010-08-25 Last modified: 2016-01-28

Priority: Not Prioritized

View other active issues in [library].

View all other issues in [library].

View all issues with C++11 status.

Discussion:

Addresses GB-65

Nothrowing `swap` operations are key to many C++ idioms, notably the common copy/swap idiom to provide the strong exception safety guarantee.

[ Resolution proposed by ballot comment ]

Where possible, all library types should provide a `swap` operation with an exception specification guaranteeing no exception shall propagate. Where `noexcept(true)` cannot be guaranteed to not terminate the program, and the `swap` in questions is a template, an exception specification with the appropriate conditional expression could be specified.

[2011-03-13: Daniel comments and drafts wording]

During a survey of the library some main categories for potential `noexcept` `swap` function could be isolated:

1. Free `swap` functions that are specified in terms of already `noexcept` `swap` member functions, like that of `valarray`.
2. Free `swap` of `std::function`, and member and free `swap` functions of stream buffers and streams where considered but rejected as good candidates, because of the danger to potentially impose requirements of existing implementations. These functions could be reconsidered as candidates in the future.

Negative list:

1. Algorithms related to swap, like `iter_swap`, have not been touched, because there are no fundamental exceptions constraints on iterator operations in general (only for specific types, like library container iterators)

While evaluating the current state of `swap` functions of library components it was observed that several conditional `noexcept` functions have turned into unconditional ones, e.g. in the header `<utility>` synopsis:

>     template<class T> void swap(T& a, T& b) noexcept;

The suggested resolution shown below also attempts to fix these cases.

[2011-03-22 Daniel redrafts to satisfy new criteria for applying `noexcept`. Parts resolved by N3263-v2 and D3267 are not added here.]

Proposed resolution:

1. Edit 20.2 [utility] p. 2, header `<utility>` synopsis and 20.2.2 [utility.swap] before p. 1, as indicated (The intent is to fix an editorial omission):

   >     template<class T> void swap(T& a, T& b) noexcept(see below);

2. Edit the prototype declaration in 20.4.2 [pairs.pair] before p. 34 as indicated (The intent is to fix an editorial omission):

   >     void swap(pair& p) noexcept(see below);

3. Edit 20.5.1 [tuple.general] p. 2 header `<tuple>` synopsis and 20.5.10 [tuple.special] before p. 1 as indicated (The intent is to fix an editorial omission):

   >     template <class... Types>
   >     void swap(tuple<Types...>& x, tuple<Types...>& y) noexcept(see below);

4. Edit 20.5.3 [tuple.tuple], class template `tuple` synopsis and 20.5.3.3 [tuple.swap] before p. 1 as indicated (The intent is to fix an editorial omission):

   >     void swap(tuple&) noexcept(see below);

5. Edit 20.10.2 [memory.syn] p. 1, header `<memory>` synopsis as indicated (The intent is to fix an editorial omission of the proposing paper N3195).

   >     template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;

6. Edit header `<valarray>` synopsis, 26.7.1 [valarray.syn] and 26.7.3.4 [valarray.special] before p. 1 as indicated [Drafting comment: The corresponding member swap is already noexcept]:

   >     template<class T> void swap(valarray<T>&, valarray<T>&) noexcept;