1//===----------------------------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10
11// template<class U, class G>
12// constexpr explicit(!is_convertible_v<G, E>) expected(expected<U, G>&& rhs);
13//
14// Let GF be G
15//
16// Constraints:
17// - is_void_v<U> is true; and
18// - is_constructible_v<E, GF> is true; and
19// - is_constructible_v<unexpected<E>, expected<U, G>&> is false; and
20// - is_constructible_v<unexpected<E>, expected<U, G>> is false; and
21// - is_constructible_v<unexpected<E>, const expected<U, G>&> is false; and
22// - is_constructible_v<unexpected<E>, const expected<U, G>> is false.
23//
24// Effects: If rhs.has_value() is false, direct-non-list-initializes unex with std::forward<GF>(rhs.error()).
25//
26// Postconditions: rhs.has_value() is unchanged; rhs.has_value() == this->has_value() is true.
27//
28// Throws: Any exception thrown by the initialization of unex.
29
30#include <cassert>
31#include <concepts>
32#include <expected>
33#include <type_traits>
34#include <utility>
35
36#include "MoveOnly.h"
37#include "test_macros.h"
38#include "../../types.h"
39
40// Test Constraints:
41template <class T1, class Err1, class T2, class Err2>
42concept canCstrFromExpected = std::is_constructible_v<std::expected<T1, Err1>, std::expected<T2, Err2>&&>;
43
44struct CtorFromInt {
45 CtorFromInt(int);
46};
47
48static_assert(canCstrFromExpected<void, CtorFromInt, void, int>);
49
50struct NoCtorFromInt {};
51
52// !is_void_v<U>
53static_assert(!canCstrFromExpected<void, int, int, int>);
54
55// !is_constructible_v<E, GF>
56static_assert(!canCstrFromExpected<void, NoCtorFromInt, void, int>);
57
58template <class T>
59struct CtorFrom {
60 explicit CtorFrom(int)
61 requires(!std::same_as<T, int>);
62 explicit CtorFrom(T);
63 explicit CtorFrom(auto&&) = delete;
64};
65
66// Note for below 4 tests, because their E is constructible from cvref of std::expected<void, int>,
67// unexpected<E> will be constructible from cvref of std::expected<void, int>
68// is_constructible_v<unexpected<E>, expected<U, G>&>
69static_assert(!canCstrFromExpected<void, CtorFrom<std::expected<void, int>&>, void, int>);
70
71// is_constructible_v<unexpected<E>, expected<U, G>>
72static_assert(!canCstrFromExpected<void, CtorFrom<std::expected<void, int>&&>, void, int>);
73
74// is_constructible_v<unexpected<E>, const expected<U, G>&> is false
75static_assert(!canCstrFromExpected<void, CtorFrom<std::expected<void, int> const&>, void, int>);
76
77// is_constructible_v<unexpected<E>, const expected<U, G>>
78static_assert(!canCstrFromExpected<void, CtorFrom<std::expected<void, int> const&&>, void, int>);
79
80// test explicit
81static_assert(std::is_convertible_v<std::expected<void, int>&&, std::expected<void, long>>);
82
83// !is_convertible_v<GF, E>.
84static_assert(std::is_constructible_v<std::expected<void, CtorFrom<int>>, std::expected<void, int>&&>);
85static_assert(!std::is_convertible_v<std::expected<void, int>&&, std::expected<void, CtorFrom<int>>>);
86
87struct Data {
88 MoveOnly data;
89 constexpr Data(MoveOnly&& m) : data(std::move(m)) {}
90};
91
92constexpr bool test() {
93 // convert the error
94 {
95 std::expected<void, MoveOnly> e1(std::unexpect, 5);
96 std::expected<void, Data> e2 = std::move(e1);
97 assert(!e2.has_value());
98 assert(e2.error().data.get() == 5);
99 assert(!e1.has_value());
100 assert(e1.error().get() == 0);
101 }
102
103 // convert TailClobberer
104 {
105 std::expected<void, TailClobbererNonTrivialMove<1>> e1(std::unexpect);
106 std::expected<void, TailClobberer<1>> e2 = std::move(e1);
107 assert(!e2.has_value());
108 assert(!e1.has_value());
109 }
110
111 return true;
112}
113
114void testException() {
115#ifndef TEST_HAS_NO_EXCEPTIONS
116 struct ThrowingInt {
117 ThrowingInt(int) { throw Except{}; }
118 };
119
120 // throw on converting error
121 {
122 const std::expected<void, int> e1(std::unexpect);
123 try {
124 [[maybe_unused]] std::expected<void, ThrowingInt> e2 = e1;
125 assert(false);
126 } catch (Except) {
127 }
128 }
129
130#endif // TEST_HAS_NO_EXCEPTIONS
131}
132
133int main(int, char**) {
134 test();
135 static_assert(test());
136 testException();
137 return 0;
138}
139

source code of libcxx/test/std/utilities/expected/expected.void/ctor/ctor.convert.move.pass.cpp