Debian Codegraph @

libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.copy.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 // constexpr expected(const expected& rhs);
11 //
12 // Effects: If rhs.has_value() is true, direct-non-list-initializes val with *rhs.
13 // Otherwise, direct-non-list-initializes unex with rhs.error().
14 //
15 // Postconditions: rhs.has_value() == this->has_value().
16 //
17 // Throws: Any exception thrown by the initialization of val or unex.
18 //
19 // Remarks: This constructor is defined as deleted unless
20 // - is_copy_constructible_v<T> is true and
21 // - is_copy_constructible_v<E> is true.
22 //
23 // This constructor is trivial if
24 // - is_trivially_copy_constructible_v<T> is true and
25 // - is_trivially_copy_constructible_v<E> is true.
26
27 #include <cassert>
28 #include <expected>
29 #include <type_traits>
30 #include <utility>
31
32 #include "test_macros.h"
33 #include "../../types.h"
34
35 struct NonCopyable {
36 NonCopyable(const NonCopyable&) = delete;
37 };
38
39 struct CopyableNonTrivial {
40 int i;
41 constexpr CopyableNonTrivial(int ii) : i(ii) {}
42 constexpr CopyableNonTrivial(const CopyableNonTrivial& o) { i = o.i; }
43 friend constexpr bool operator==(const CopyableNonTrivial&, const CopyableNonTrivial&) = default;
44 };
45
46 // Test: This constructor is defined as deleted unless
47 // - is_copy_constructible_v<T> is true and
48 // - is_copy_constructible_v<E> is true.
49 static_assert(std::is_copy_constructible_v<std::expected<int, int>>);
50 static_assert(std::is_copy_constructible_v<std::expected<CopyableNonTrivial, int>>);
51 static_assert(std::is_copy_constructible_v<std::expected<int, CopyableNonTrivial>>);
52 static_assert(std::is_copy_constructible_v<std::expected<CopyableNonTrivial, CopyableNonTrivial>>);
53 static_assert(!std::is_copy_constructible_v<std::expected<NonCopyable, int>>);
54 static_assert(!std::is_copy_constructible_v<std::expected<int, NonCopyable>>);
55 static_assert(!std::is_copy_constructible_v<std::expected<NonCopyable, NonCopyable>>);
56
57 // Test: This constructor is trivial if
58 // - is_trivially_copy_constructible_v<T> is true and
59 // - is_trivially_copy_constructible_v<E> is true.
60 static_assert(std::is_trivially_copy_constructible_v<std::expected<int, int>>);
61 static_assert(!std::is_trivially_copy_constructible_v<std::expected<CopyableNonTrivial, int>>);
62 static_assert(!std::is_trivially_copy_constructible_v<std::expected<int, CopyableNonTrivial>>);
63 static_assert(!std::is_trivially_copy_constructible_v<std::expected<CopyableNonTrivial, CopyableNonTrivial>>);
64
65 struct Any {
66 constexpr Any() = default;
67 constexpr Any(const Any&) = default;
68 constexpr Any& operator=(const Any&) = default;
69
70 template <class T>
71 requires(!std::is_same_v<Any, std::decay_t<T>> && std::is_copy_constructible_v<std::decay_t<T>>)
72 constexpr Any(T&&) {}
73 };
74
75 constexpr bool test() {
76 // copy the value non-trivial
77 {
78 const std::expected<CopyableNonTrivial, int> e1(5);
79 auto e2 = e1;
80 assert(e2.has_value());
81 assert(e2.value().i == 5);
82 }
83
84 // copy the error non-trivial
85 {
86 const std::expected<int, CopyableNonTrivial> e1(std::unexpect, 5);
87 auto e2 = e1;
88 assert(!e2.has_value());
89 assert(e2.error().i == 5);
90 }
91
92 // copy the value trivial
93 {
94 const std::expected<int, int> e1(5);
95 auto e2 = e1;
96 assert(e2.has_value());
97 assert(e2.value() == 5);
98 }
99
100 // copy the error trivial
101 {
102 const std::expected<int, int> e1(std::unexpect, 5);
103 auto e2 = e1;
104 assert(!e2.has_value());
105 assert(e2.error() == 5);
106 }
107
108 // copy TailClobberer as value
109 {
110 const std::expected<TailClobberer<0>, bool> e1;
111 auto e2 = e1;
112 assert(e2.has_value());
113 }
114
115 // copy TailClobberer as error
116 {
117 const std::expected<bool, TailClobberer<1>> e1(std::unexpect);
118 auto e2 = e1;
119 assert(!e2.has_value());
120 }
121
122 {
123 // TODO(LLVM 20): Remove once we drop support for Clang 17
124 #if defined(TEST_CLANG_VER) && TEST_CLANG_VER >= 1800
125 // https://github.com/llvm/llvm-project/issues/92676
126 std::expected<Any, int> e1;
127 auto e2 = e1;
128 assert(e2.has_value());
129 #endif
130 }
131
132 return true;
133 }
134
135 void testException() {
136 #ifndef TEST_HAS_NO_EXCEPTIONS
137 struct Throwing {
138 Throwing() = default;
139 Throwing(const Throwing&) { throw Except{}; }
140 };
141
142 // throw on copying value
143 {
144 const std::expected<Throwing, int> e1;
145 try {
146 [[maybe_unused]] auto e2 = e1;
147 assert(false);
148 } catch (Except) {
149 }
150 }
151
152 // throw on copying error
153 {
154 const std::expected<int, Throwing> e1(std::unexpect);
155 try {
156 [[maybe_unused]] auto e2 = e1;
157 assert(false);
158 } catch (Except) {
159 }
160 }
161
162 #endif // TEST_HAS_NO_EXCEPTIONS
163 }
164
165 int main(int, char**) {
166 test();
167 static_assert(test());
168 testException();
169 return 0;
170 }