Title
swap should not throw
Status
c++11
Section
[library]
Submitter
BSI

Created on 2010-08-25.00:00:00 last changed 161 months ago

Messages

Date: 2011-03-23.13:28:27

Proposed resolution:

  1. Edit [utility] p. 2, header <utility> synopsis and [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 [pairs.pair] before p. 34 as indicated (The intent is to fix an editorial omission):

    void swap(pair& p) noexcept(see below);
    
  3. Edit [tuple.general] p. 2 header <tuple> synopsis and [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 [tuple.tuple], class template tuple synopsis and [tuple.swap] before p. 1 as indicated (The intent is to fix an editorial omission):

    void swap(tuple&) noexcept(see below);
    
  5. Edit [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, [valarray.syn] and [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;
    
Date: 2011-03-22.00:00:00

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

Date: 2011-03-13.00:00:00

[ 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.

Date: 2010-10-24.22:33:31

[ 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.

Date: 2010-10-24.10:50:39

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.

History
Date User Action Args
2011-08-23 20:07:26adminsetstatus: wp -> c++11
2011-04-11 11:23:23adminsetstatus: immediate -> wp
2011-03-23 18:19:51adminsetstatus: open -> immediate
2011-03-22 23:36:56adminsetmessages: + msg5662
2011-03-13 18:26:09adminsetmessages: + msg5648
2011-03-13 18:26:09adminsetmessages: + msg5647
2010-10-24 22:33:31adminsetmessages: + msg5056
2010-10-24 10:50:39adminsetmessages: + msg4978
2010-08-25 00:00:00admincreate