This page is a snapshot from the LWG issues list, see theLibrary Active Issues List for more information and the meaning ofC++11 status.
swap should not throwSection: 16[library]Status:C++11Submitter: BSIOpened: 2010-08-25Last modified: 2016-01-28
Priority:Not Prioritized
View otheractive issues in [library].
View all otherissues in [library].
View all issues withC++11 status.
Discussion:
Addresses GB-65
Nothrowingswap operations are key to many C++ idioms,notably the common copy/swap idiom to provide thestrong exception safety guarantee.
[Resolution proposed by ballot comment]
Where possible, all library types should provide aswap operation with an exception specificationguaranteeing no exception shall propagate.Wherenoexcept(true) cannot be guaranteed tonot terminate the program, and theswap inquestions is a template, an exception specificationwith the appropriate conditional expression couldbe specified.
[2011-03-13: Daniel comments and drafts wording]
During a survey of the library some main categories forpotentialnoexceptswap function could be isolated:
swap functions that are specified in terms of alreadynoexceptswap member functions, like that ofvalarray.swap ofstd::function, and member and freeswap 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:
iter_swap, havenot 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 ofswap functions of library components it was observed that several conditionalnoexceptfunctions have turned into unconditional ones, e.g. in theheader<utility> synopsis:
template<class T> void swap(T& a, T& b) noexcept;
The suggested resolution shown below also attempts to fixthese cases.
[2011-03-22 Daniel redrafts to satisfy new criteria for applyingnoexcept.Parts resolved by N3263-v2 and D3267 are not added here.]
Proposed resolution:
Edit 22.2[utility] p. 2, header<utility> synopsisand 22.2.2[utility.swap] before p. 1, as indicated (The intent is to fix an editorialomission):
template<class T> void swap(T& a, T& b) noexcept(see below);
Edit the prototype declaration in 22.3.2[pairs.pair] before p. 34 as indicated (The intent is to fix an editorial omission):
void swap(pair& p) noexcept(see below);
Edit 22.4.1[tuple.general] p. 2 header<tuple> synopsisand 22.4.12[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);
Edit 22.4.4[tuple.tuple], class templatetuple synopsisand22.4.4.4[tuple.swap] before p. 1 as indicated (The intent is to fix an editorial omission):
void swap(tuple&) noexcept(see below);
Edit 20.2.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;
Edit header<valarray> synopsis, 29.6.1[valarray.syn]and 29.6.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;