8

The C++ standard guarantees that std::swap will throw no exception. However, what if an object to swap throws an exception during swapping? Next, how should the caller find an exception has happened? and what measures should the caller take?

PS: It is very common that a constructor throws an exception.

struct A
{
    A(const A&)
    {
        throw 1;
    }

    A& operator =(const A&)
    {
        throw 2;
        return *this;
    }
};

int main()
{
    A a1, a2;
    std::swap(a1, a2); // An exception happened, but the caller doesn't know.
    // How to do here ???
}
4
  • 8
    10 lashes for A's author for not providing a non-throwing swap(A, A) overload Jan 30, 2013 at 10:17
  • @rhalbersma: not that providing an overload would help, since the questioner's code fully-qualifies the call std::swap. Jan 30, 2013 at 10:43
  • @SteveJessop see my answer why indeed that would be much better. Jan 30, 2013 at 10:45
  • "It is very common that a constructor throws an exception." It is not very common for a copy constructor to throw an exception (outside of memory allocation errors). Jan 30, 2013 at 14:05

2 Answers 2

18

The C++ standard guarantees that std::swap will throw no exception.

No, it doesn't. See 20.2.2 or the reference. There are two noexcept specifications for the two std::swap overloads:

template<class T> void swap(T& a, T& b)
noexcept(noexcept(
    std::is_nothrow_move_constructible<T>::value &&
    std::is_nothrow_move_assignable<T>::value
))

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

When these conditions aren't satisfied, std::swap can throw and you can catch it.


In case of the class you have presented, the predicates std::is_nothrow_move_constructible and std::is_nothrow_move_assignable are false, so the instantiation std::swap<A> doesn't have the no-throw guarantee. It's perfectly legal to catch exceptions from this swap.

3
  • how can these conditions not be satisfied? there are no other overloads without a noexcept specification. You cannot catch an exception thrown from within std::swap Jan 30, 2013 at 10:34
  • 3
    swap has a conditional noexcept specification. It is noexcept when instantiated with a type for which move construction and move assignment are noexcept. Otherwise it can throw anything.
    – JoergB
    Jan 30, 2013 at 11:06
  • 1
    @rhalbersma cppreference has some nice descriptions how the noexpect specifier and the noexpect operator work, please read up
    – Kos
    Jan 30, 2013 at 11:15
10

The standard does not generally guarantee that swap doesn't throw.

From 20.2.2/1:

template void swap(T& a, T& b) noexcept(see below);

Remark: The expression inside noexcept is equivalent to:

 is_nothrow_move_constructible<T>::value &&
 is_nothrow_move_assignable<T>::value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.