1349. swap should not throw

Section: 20 [library] Status: C++11 Submitter: BSI Opened: 2010-08-25 Last modified: 2016-02-10

Priority: Not Prioritized

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 23.2 [utility] p. 2, header <utility> synopsis and 23.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 23.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 23.5.1 [tuple.general] p. 2 header <tuple> synopsis and 23.5.3.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 23.5.3 [tuple.tuple], class template tuple synopsis and 23.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 23.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, 29.7.1 [valarray.syn] and 29.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;