Debian Codegraph @

libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.u.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 // template<class U = T>
11 // constexpr explicit(!is_convertible_v<U, T>) expected(U&& v);
12 //
13 // Constraints:
14 // - is_same_v<remove_cvref_t<U>, in_place_t> is false; and
15 // - is_same_v<expected, remove_cvref_t<U>> is false; and
16 // - remove_cvref_t<U> is not a specialization of unexpected; and
17 // - is_constructible_v<T, U> is true.
18 //
19 // Effects: Direct-non-list-initializes val with std::forward<U>(v).
20 //
21 // Postconditions: has_value() is true.
22 //
23 // Throws: Any exception thrown by the initialization of val.
24
25 #include <cassert>
26 #include <expected>
27 #include <type_traits>
28 #include <utility>
29
30 #include "MoveOnly.h"
31 #include "test_macros.h"
32 #include "../../types.h"
33
34 // Test Constraints:
35 static_assert(std::is_constructible_v<std::expected<int, int>, int>);
36
37 // is_same_v<remove_cvref_t<U>, in_place_t>
38 struct FromJustInplace {
39 FromJustInplace(std::in_place_t);
40 };
41 static_assert(!std::is_constructible_v<std::expected<FromJustInplace, int>, std::in_place_t>);
42 static_assert(!std::is_constructible_v<std::expected<FromJustInplace, int>, std::in_place_t const&>);
43
44 // is_same_v<expected, remove_cvref_t<U>>
45 // Note that result is true because it is covered by the constructors that take expected
46 static_assert(std::is_constructible_v<std::expected<int, int>, std::expected<int, int>&>);
47
48 // remove_cvref_t<U> is a specialization of unexpected
49 // Note that result is true because it is covered by the constructors that take unexpected
50 static_assert(std::is_constructible_v<std::expected<int, int>, std::unexpected<int>&>);
51
52 // !is_constructible_v<T, U>
53 struct foo {};
54 static_assert(!std::is_constructible_v<std::expected<int, int>, foo>);
55
56 // test explicit(!is_convertible_v<U, T>)
57 struct NotConvertible {
58 explicit NotConvertible(int);
59 };
60 static_assert(std::is_convertible_v<int, std::expected<int, int>>);
61 static_assert(!std::is_convertible_v<int, std::expected<NotConvertible, int>>);
62
63 struct CopyOnly {
64 int i;
65 constexpr CopyOnly(int ii) : i(ii) {}
66 CopyOnly(const CopyOnly&) = default;
67 CopyOnly(CopyOnly&&) = delete;
68 friend constexpr bool operator==(const CopyOnly& mi, int ii) { return mi.i == ii; }
69 };
70
71 struct BaseError {};
72 struct DerivedError : BaseError {};
73
74 template <class T, class E = int>
75 constexpr void testInt() {
76 std::expected<T, E> e(5);
77 assert(e.has_value());
78 assert(e.value() == 5);
79 }
80
81 template <class T, class E = int>
82 constexpr void testLValue() {
83 T t(5);
84 std::expected<T, E> e(t);
85 assert(e.has_value());
86 assert(e.value() == 5);
87 }
88
89 template <class T, class E = int>
90 constexpr void testRValue() {
91 std::expected<T, E> e(T(5));
92 assert(e.has_value());
93 assert(e.value() == 5);
94 }
95
96 constexpr bool test() {
97 testInt<int>();
98 testInt<CopyOnly>();
99 testInt<MoveOnly>();
100 testInt<TailClobberer<0>, bool>();
101 testLValue<int>();
102 testLValue<CopyOnly>();
103 testLValue<TailClobberer<0>, bool>();
104 testRValue<int>();
105 testRValue<MoveOnly>();
106 testRValue<TailClobberer<0>, bool>();
107
108 // Test default template argument.
109 // Without it, the template parameter cannot be deduced from an initializer list
110 {
111 struct Bar {
112 int i;
113 int j;
114 constexpr Bar(int ii, int jj) : i(ii), j(jj) {}
115 };
116
117 std::expected<Bar, int> e({5, 6});
118 assert(e.value().i == 5);
119 assert(e.value().j == 6);
120 }
121
122 // https://cplusplus.github.io/LWG/issue3836
123
124 // Test &
125 {
126 std::expected<bool, DerivedError> e1(false);
127 std::expected<bool, BaseError> e2(e1);
128 assert(e2.has_value());
129 assert(!e2.value()); // yes, e2 holds "false" since LWG3836
130 }
131
132 // Test &&
133 {
134 std::expected<bool, DerivedError> e1(false);
135 std::expected<bool, BaseError> e2(std::move(e1));
136 assert(e2.has_value());
137 assert(!e2.value()); // yes, e2 holds "false" since LWG3836
138 }
139
140 // Test const&
141 {
142 const std::expected<bool, DerivedError> e1(false);
143 std::expected<bool, BaseError> e2(e1);
144 assert(e2.has_value());
145 assert(!e2.value()); // yes, e2 holds "false" since LWG3836
146 }
147
148 // Test const&&
149 {
150 const std::expected<bool, DerivedError> e1(false);
151 std::expected<bool, BaseError> e2(std::move(e1));
152 assert(e2.has_value());
153 assert(!e2.value()); // yes, e2 holds "false" since LWG3836
154 }
155 return true;
156 }
157
158 void testException() {
159 #ifndef TEST_HAS_NO_EXCEPTIONS
160 struct Throwing {
161 Throwing(int) { throw Except{}; };
162 };
163
164 try {
165 std::expected<Throwing, int> u(5);
166 assert(false);
167 } catch (Except) {
168 }
169 #endif // TEST_HAS_NO_EXCEPTIONS
170 }
171
172 int main(int, char**) {
173 test();
174 static_assert(test());
175 testException();
176 return 0;
177 }