//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 // constexpr void swap(unexpected& other) noexcept(is_nothrow_swappable_v); // // Mandates: is_swappable_v is true. // // Effects: Equivalent to: using std::swap; swap(unex, other.unex); #include #include #include #include // test noexcept struct NoexceptSwap { friend void swap(NoexceptSwap&, NoexceptSwap&) noexcept; }; struct MayThrowSwap { friend void swap(MayThrowSwap&, MayThrowSwap&); }; template concept MemberSwapNoexcept = requires(T& t1, T& t2) { { t1.swap(t2) } noexcept; }; static_assert(MemberSwapNoexcept>); static_assert(!MemberSwapNoexcept>); struct ADLSwap { constexpr ADLSwap(int ii) : i(ii) {} ADLSwap& operator=(const ADLSwap&) = delete; int i; constexpr friend void swap(ADLSwap& x, ADLSwap& y) { std::swap(x.i, y.i); } }; constexpr bool test() { // using std::swap; { std::unexpected unex1(5); std::unexpected unex2(6); unex1.swap(unex2); assert(unex1.error() == 6); assert(unex2.error() == 5); } // adl swap { std::unexpected unex1(5); std::unexpected unex2(6); unex1.swap(unex2); assert(unex1.error().i == 6); assert(unex2.error().i == 5); } return true; } int main(int, char**) { test(); static_assert(test()); return 0; }