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 Err = E> |
11 | // constexpr explicit unexpected(Err&& e); |
12 | // |
13 | // Constraints: |
14 | // - is_same_v<remove_cvref_t<Err>, unexpected> is false; and |
15 | // - is_same_v<remove_cvref_t<Err>, in_place_t> is false; and |
16 | // - is_constructible_v<E, Err> is true. |
17 | // |
18 | // Effects: Direct-non-list-initializes unex with std::forward<Err>(e). |
19 | // Throws: Any exception thrown by the initialization of unex. |
20 | |
21 | #include <cassert> |
22 | #include <concepts> |
23 | #include <expected> |
24 | #include <utility> |
25 | |
26 | #include "test_macros.h" |
27 | |
28 | // Test Constraints: |
29 | static_assert(std::constructible_from<std::unexpected<int>, int>); |
30 | |
31 | // is_same_v<remove_cvref_t<Err>, unexpected> |
32 | struct CstrFromUnexpected { |
33 | CstrFromUnexpected(CstrFromUnexpected const&) = delete; |
34 | CstrFromUnexpected(std::unexpected<CstrFromUnexpected> const&); |
35 | }; |
36 | static_assert(!std::constructible_from<std::unexpected<CstrFromUnexpected>, std::unexpected<CstrFromUnexpected>>); |
37 | |
38 | // is_same_v<remove_cvref_t<Err>, in_place_t> |
39 | struct CstrFromInplace { |
40 | CstrFromInplace(std::in_place_t); |
41 | }; |
42 | static_assert(!std::constructible_from<std::unexpected<CstrFromInplace>, std::in_place_t>); |
43 | |
44 | // !is_constructible_v<E, Err> |
45 | struct Foo {}; |
46 | static_assert(!std::constructible_from<std::unexpected<Foo>, int>); |
47 | |
48 | // test explicit |
49 | static_assert(std::convertible_to<int, int>); |
50 | static_assert(!std::convertible_to<int, std::unexpected<int>>); |
51 | |
52 | struct Error { |
53 | int i; |
54 | constexpr Error(int ii) : i(ii) {} |
55 | constexpr Error(const Error& other) : i(other.i) {} |
56 | constexpr Error(Error&& other) : i(other.i) { other.i = 0; } |
57 | Error(std::initializer_list<Error>) { assert(false); } |
58 | }; |
59 | |
60 | constexpr bool test() { |
61 | // lvalue |
62 | { |
63 | Error e(5); |
64 | std::unexpected<Error> unex(e); |
65 | assert(unex.error().i == 5); |
66 | assert(e.i == 5); |
67 | } |
68 | |
69 | // rvalue |
70 | { |
71 | Error e(5); |
72 | std::unexpected<Error> unex(std::move(e)); |
73 | assert(unex.error().i == 5); |
74 | assert(e.i == 0); |
75 | } |
76 | |
77 | // Direct-non-list-initializes: does not trigger initializer_list overload |
78 | { |
79 | Error e(5); |
80 | [[maybe_unused]] std::unexpected<Error> unex(e); |
81 | } |
82 | |
83 | // Test default template argument. |
84 | // Without it, the template parameter cannot be deduced from an initializer list |
85 | { |
86 | struct Bar { |
87 | int i; |
88 | int j; |
89 | constexpr Bar(int ii, int jj) : i(ii), j(jj) {} |
90 | }; |
91 | std::unexpected<Bar> ue({5, 6}); |
92 | assert(ue.error().i == 5); |
93 | assert(ue.error().j == 6); |
94 | } |
95 | |
96 | return true; |
97 | } |
98 | |
99 | void testException() { |
100 | #ifndef TEST_HAS_NO_EXCEPTIONS |
101 | struct Except {}; |
102 | |
103 | struct Throwing { |
104 | Throwing() = default; |
105 | Throwing(const Throwing&) { throw Except{}; } |
106 | }; |
107 | |
108 | Throwing t; |
109 | try { |
110 | std::unexpected<Throwing> u(t); |
111 | assert(false); |
112 | } catch (Except) { |
113 | } |
114 | #endif // TEST_HAS_NO_EXCEPTIONS |
115 | } |
116 | |
117 | int main(int, char**) { |
118 | test(); |
119 | static_assert(test()); |
120 | testException(); |
121 | return 0; |
122 | } |
123 | |