1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
11 // constexpr void swap(expected& rhs) noexcept(see below);
14 // is_swappable_v<E> is true and is_move_constructible_v<E> is true.
16 // Throws: Any exception thrown by the expressions in the Effects.
18 // Remarks: The exception specification is equivalent to:
19 // is_nothrow_move_constructible_v<E> && is_nothrow_swappable_v<E>.
23 #include <type_traits>
26 #include "../../types.h"
27 #include "test_macros.h"
31 concept HasMemberSwap
= requires(std::expected
<void, E
> x
, std::expected
<void, E
> y
) { x
.swap(y
); };
33 static_assert(HasMemberSwap
<int>);
35 struct NotSwappable
{};
36 void swap(NotSwappable
&, NotSwappable
&) = delete;
39 static_assert(!HasMemberSwap
<NotSwappable
>);
41 struct NotMoveConstructible
{
42 NotMoveConstructible(NotMoveConstructible
&&) = delete;
43 friend void swap(NotMoveConstructible
&, NotMoveConstructible
&) {}
46 // !is_move_constructible_v<E>
47 static_assert(!HasMemberSwap
<NotMoveConstructible
>);
51 MoveMayThrow(MoveMayThrow
&&) noexcept(false);
52 friend void swap(MoveMayThrow
&, MoveMayThrow
&) noexcept
{}
56 concept MemberSwapNoexcept
= //
57 requires(std::expected
<void, E
> x
, std::expected
<void, E
> y
) {
58 { x
.swap(y
) } noexcept
;
61 static_assert(MemberSwapNoexcept
<int>);
63 // !is_nothrow_move_constructible_v<E>
64 static_assert(!MemberSwapNoexcept
<MoveMayThrow
>);
67 friend void swap(SwapMayThrow
&, SwapMayThrow
&) noexcept(false) {}
70 // !is_nothrow_swappable_v<E>
71 static_assert(!MemberSwapNoexcept
<SwapMayThrow
>);
73 constexpr bool test() {
74 // this->has_value() && rhs.has_value()
76 std::expected
<void, int> x
;
77 std::expected
<void, int> y
;
80 assert(x
.has_value());
81 assert(y
.has_value());
84 // !this->has_value() && !rhs.has_value()
86 std::expected
<void, ADLSwap
> x(std::unexpect
, 5);
87 std::expected
<void, ADLSwap
> y(std::unexpect
, 10);
90 assert(!x
.has_value());
91 assert(x
.error().i
== 10);
92 assert(x
.error().adlSwapCalled
);
93 assert(!y
.has_value());
94 assert(y
.error().i
== 5);
95 assert(y
.error().adlSwapCalled
);
98 // this->has_value() && !rhs.has_value()
101 std::expected
<void, Traced
> e1(std::in_place
);
102 std::expected
<void, Traced
> e2(std::unexpect
, s
, 10);
106 assert(!e1
.has_value());
107 assert(e1
.error().data_
== 10);
108 assert(e2
.has_value());
110 assert(s
.moveCtorCalled
);
111 assert(s
.dtorCalled
);
114 // !this->has_value() && rhs.has_value()
117 std::expected
<void, Traced
> e1(std::unexpect
, s
, 10);
118 std::expected
<void, Traced
> e2(std::in_place
);
122 assert(e1
.has_value());
123 assert(!e2
.has_value());
124 assert(e2
.error().data_
== 10);
126 assert(s
.moveCtorCalled
);
127 assert(s
.dtorCalled
);
132 std::expected
<void, TailClobbererNonTrivialMove
<1>> x(std::in_place
);
133 std::expected
<void, TailClobbererNonTrivialMove
<1>> y(std::unexpect
);
137 // The next line would fail if adjusting the "has value" flag happened
138 // _before_ constructing the member object inside the `swap`.
139 assert(!x
.has_value());
140 assert(y
.has_value());
143 // CheckForInvalidWrites
146 CheckForInvalidWrites
<true, true> x(std::unexpect
);
147 CheckForInvalidWrites
<true, true> y
;
155 CheckForInvalidWrites
<false, true> x(std::unexpect
);
156 CheckForInvalidWrites
<false, true> y
;
168 void testException() {
169 #ifndef TEST_HAS_NO_EXCEPTIONS
170 // !e1.has_value() && e2.has_value()
172 bool e1Destroyed
= false;
173 std::expected
<void, ThrowOnMove
> e1(std::unexpect
, e1Destroyed
);
174 std::expected
<void, ThrowOnMove
> e2(std::in_place
);
179 assert(!e1
.has_value());
180 assert(e2
.has_value());
181 assert(!e1Destroyed
);
185 // e1.has_value() && !e2.has_value()
187 bool e2Destroyed
= false;
188 std::expected
<void, ThrowOnMove
> e1(std::in_place
);
189 std::expected
<void, ThrowOnMove
> e2(std::unexpect
, e2Destroyed
);
194 assert(e1
.has_value());
195 assert(!e2
.has_value());
196 assert(!e2Destroyed
);
202 std::expected
<void, TailClobbererNonTrivialMove
<0, false, true>> x(std::in_place
);
203 std::expected
<void, TailClobbererNonTrivialMove
<0, false, true>> y(std::unexpect
);
208 // This would fail if `TailClobbererNonTrivialMove<0, false, true>`
209 // clobbered the flag before throwing the exception.
210 assert(x
.has_value());
211 assert(!y
.has_value());
214 #endif // TEST_HAS_NO_EXCEPTIONS
217 int main(int, char**) {
219 static_assert(test());