Search code examples
c++noexcept

C++ ISO noexcept of noexcept


In the C++ standard there is the following definition:

template <class T, size_t N> void swap(T (&a)[N], T (&b)[N])
      noexcept(noexcept(swap(*a, *b)));

What does noexcept(noexcept(swap(*a, *b))) do?


Solution

  • Having the noexcept(x) specifier in a function declaration means that the function is non-throwing if and only if x evaluates to true.

    noexcept(y) can also be used as an operator, evaluating to true if y is a non-throwing expression, and to false if y can potentially throw.

    Combined, this means void foo() noexcept(noexcept(y)); means: foo is non-throwing exactly when y is non-throwing.

    In the case in the question, the function template swap for arrays is declared to be non-throwing if and only if swapping individual members of the arrays is non-throwing, which makes sense.