Boost logo

Boost :

From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2019-06-27 15:43:34

On 6/27/19 6:16 PM, Robert Ramey via Boost wrote:
> On 6/26/19 11:45 PM, degski via Boost wrote:
>> On Thu, 27 Jun 2019 at 09:35, Robert Ramey via Boost
>> <boost_at_[hidden]>
>> wrote:
>>>   From time to time I think about this.  Is swap guarenteed not to
>>> throw?
>> As from C++11 std::swap is marked noexcept, so yes. As from C++20 it's
>> also
>> constexpr, so now it can definitely cannot throw.

No, std::swap is marked conditionally noexcept if
is_nothrow_move_constructible_v<T> && is_nothrow_move_assignable_v<T>.
Though usually this condition is true.

> Hmmm - my understanding of noexcept was not that it couldn't throw but
> than any attempt to throw would result in immediate termination rather
> than an exception.  (I'm confused about noexcept as well).

Both statements are true:

- From the noexcept function perspective, if an exception tries to
propagate out of the function scope, call std::terminate().
- From the caller's perspective, a noexcept function will never throw.

> In anycase I
> should have phrased the above as:
> Is swap guaranteed to succeed?

In general, no. It is only guaranteed to succeed if it is noexcept. See
above when it is noexcept. In C++17 there is is_nothrow_swappable trait
that allows to test this.

Boost list run by bdawes at, gregod at, cpdaniel at, john at