The standard library defines an swaps for arrays and std::pair
like so:
template <class T, size_t N>
void swap(T (&a)[N],
T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
template <class T1, class T2>
struct pair {
…
void swap(pair& p) noexcept(noexcept(swap(first, p.first)) &&
noexcept(swap(second, p.second)));
…
};
Effective Modern C++ Item 14 says:
[…] whether they are
noexcept
depends on whether the expression inside thenoexcept
clauses arenoexcept
.
Given two arrays ofWidget
, for example, swapping them isnoexcept
only if swapping individual elements in the arrays isnoexcept
, i.e. if swap forWidget
isnoexcept
.
That, in turn, determines whether other swaps, such as the one for arrays of arrays ofWidget
, arenoexcept
.
Similarly, whether swapping twostd::pair
objects containingWidget
s isnoexcept
depends on whether swap forWidget
s isnoexcept
.
But from this explanation, I cannot understand why it's necessary to nest the call as noexcept(noexcept(swap(*a, *b)))
.
If the goal is "swapping two arrays should be as noexcept
as swapping elements", why doesn't noexcept(swap(*a, *b))
suffice?
Are these different overloads of noexcept()
or something?