Debian Codegraph @

libcxx/test/std/utilities/expected/expected.unexpected/swap/swap.free.pass.cpp

raw
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 // friend constexpr void swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y)));
11 //
12 // Constraints: is_swappable_v<E> is true.
13 //
14 // Effects: Equivalent to x.swap(y).
15
16 #include <cassert>
17 #include <concepts>
18 #include <expected>
19 #include <type_traits>
20 #include <utility>
21
22 // test noexcept
23 struct NoexceptSwap {
24 friend void swap(NoexceptSwap&, NoexceptSwap&) noexcept;
25 };
26
27 struct MayThrowSwap {
28 friend void swap(MayThrowSwap&, MayThrowSwap&);
29 };
30
31 template <class T>
32 concept ADLSwapNoexcept =
33 requires(T& t1, T& t2) {
34 { swap(t1, t2) } noexcept;
35 };
36
37 static_assert(ADLSwapNoexcept<std::unexpected<NoexceptSwap>>);
38 static_assert(!ADLSwapNoexcept<std::unexpected<MayThrowSwap>>);
39
40 // test constraint
41 struct NonSwappable {
42 NonSwappable& operator=(const NonSwappable&) = delete;
43 };
44
45 static_assert(std::is_swappable_v<std::unexpected<int>>);
46 static_assert(std::is_swappable_v<std::unexpected<MayThrowSwap>>);
47 static_assert(!std::is_swappable_v<std::unexpected<NonSwappable>>);
48
49 struct ADLSwap {
50 constexpr ADLSwap(int ii) : i(ii) {}
51 ADLSwap& operator=(const ADLSwap&) = delete;
52 int i;
53 constexpr friend void swap(ADLSwap& x, ADLSwap& y) { std::swap(x.i, y.i); }
54 };
55
56 constexpr bool test() {
57 std::unexpected<ADLSwap> unex1(5);
58 std::unexpected<ADLSwap> unex2(6);
59 swap(unex1, unex2);
60 assert(unex1.error().i == 6);
61 assert(unex2.error().i == 5);
62 return true;
63 }
64
65 int main(int, char**) {
66 test();
67 static_assert(test());
68 return 0;
69 }