1//===----------------------------------------------------------------------===//
2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3// See https://llvm.org/LICENSE.txt for license information.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7
8// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
9
10// constexpr void swap(unexpected& other) noexcept(is_nothrow_swappable_v<E>);
11//
12// Mandates: is_swappable_v<E> is true.
13//
14// Effects: Equivalent to: using std::swap; swap(unex, other.unex);
15
16#include <cassert>
17#include <concepts>
18#include <expected>
19#include <utility>
20
21// test noexcept
22struct NoexceptSwap {
23 friend void swap(NoexceptSwap&, NoexceptSwap&) noexcept;
24};
25
26struct MayThrowSwap {
27 friend void swap(MayThrowSwap&, MayThrowSwap&);
28};
29
30template <class T>
31concept MemberSwapNoexcept =
32 requires(T& t1, T& t2) {
33 { t1.swap(t2) } noexcept;
34 };
35
36static_assert(MemberSwapNoexcept<std::unexpected<NoexceptSwap>>);
37static_assert(!MemberSwapNoexcept<std::unexpected<MayThrowSwap>>);
38
39struct ADLSwap {
40 constexpr ADLSwap(int ii) : i(ii) {}
41 ADLSwap& operator=(const ADLSwap&) = delete;
42 int i;
43 constexpr friend void swap(ADLSwap& x, ADLSwap& y) { std::swap(a&: x.i, b&: y.i); }
44};
45
46constexpr bool test() {
47 // using std::swap;
48 {
49 std::unexpected<int> unex1(5);
50 std::unexpected<int> unex2(6);
51 unex1.swap(unex2);
52 assert(unex1.error() == 6);
53 assert(unex2.error() == 5);
54 }
55
56 // adl swap
57 {
58 std::unexpected<ADLSwap> unex1(5);
59 std::unexpected<ADLSwap> unex2(6);
60 unex1.swap(unex2);
61 assert(unex1.error().i == 6);
62 assert(unex2.error().i == 5);
63 }
64 return true;
65}
66
67int main(int, char**) {
68 test();
69 static_assert(test());
70 return 0;
71}
72

source code of libcxx/test/std/utilities/expected/expected.unexpected/swap/swap.member.pass.cpp