Boost logo

Boost :

From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2019-06-27 17:31:56


On 6/27/19 8:12 PM, Robert Ramey via Boost wrote:
> On 6/27/19 9:47 AM, Andrey Semashev via Boost wrote:
>>
>> First, people normally don't write noexcept functions knowing they
>> might throw.
>
> Right - but swap is a templated function. The obvious way to implement
> swap is via moves on type T.  So how could swap be guaranteed to succeed
> for any T?

It is not guaranteed for any T. As I wrote earlier, std::swap will only
be noexcept if the corresponding functions of T are noexcept. A custom
overload of swap for some type T may or may not be noexcept on its own
terms, of course.

> LOL - I get this.  The question is: how can one know that swap will be
> successfully returned? for a given type T.

Obviously, there's no such trait as is_buggy_move_operators<T>.

But bugs aside, this is done by testing is_nothrow_swappable_v<T>. Or,
before C++17, with a construction similar to this:

   template< typename T >
   T& make_lvalue() noexcept;

   constexpr bool is_nothrow_swappable =
     noexcept(swap(make_lvalue<T>(), make_lvalue<T>()));

> In other words, how can it make sense that swap<T> be noexcept without
> considering the specific type T?

You can't say swap() is noexcept without considering T.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk