34

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yonutix
  • 1,964
  • 1
  • 22
  • 51

1 Answers1

52

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.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • 4
    Good answer, probably one might want to read the [ref](http://en.cppreference.com/w/cpp/language/noexcept) too. – gsamaras Feb 09 '18 at 10:45