]>
Commit | Line | Data |
---|---|---|
b78e0ce2 JW |
1 | // { dg-options "-std=gnu++23" } |
2 | // { dg-do run { target c++23 } } | |
3 | ||
4 | #include <expected> | |
5 | #include <testsuite_hooks.h> | |
6 | ||
7 | static_assert( sizeof(std::unexpected<char>) == 1 ); | |
8 | ||
9 | constexpr bool | |
10 | test() | |
11 | { | |
12 | std::unexpected<int> u1(1); | |
13 | VERIFY( u1.error() == 1 ); | |
14 | ||
15 | std::unexpected<int> u2(std::in_place, 2); | |
16 | VERIFY( u2.error() == 2 ); | |
17 | ||
18 | struct X | |
19 | { | |
20 | constexpr X(int i, int j) : n(i+j) { } | |
21 | constexpr X(std::initializer_list<int> l, void*) : n(l.size()) { } | |
22 | ||
23 | constexpr X(const X&) = default; | |
24 | constexpr X(X&& x) :n(x.n) { x.n = -1; } | |
25 | ||
26 | constexpr X& operator=(const X&) = default; | |
27 | constexpr X& operator=(X&& x) { n = x.n; x.n = -1; return *this; } | |
28 | ||
29 | constexpr bool operator==(const X&) const = default; | |
30 | constexpr bool operator==(int i) const { return n == i; } | |
31 | ||
32 | int n; | |
33 | }; | |
34 | ||
35 | std::unexpected<X> u3(std::in_place, 2, 1); | |
36 | VERIFY( u3.error() == 3 ); | |
37 | ||
38 | std::unexpected<X> u4(std::in_place, {1,2,3,4}, nullptr); | |
39 | VERIFY( u4.error() == 4 ); | |
40 | ||
41 | std::unexpected<X> u5(u4); | |
42 | VERIFY( u5.error() == 4 ); | |
43 | VERIFY( u4.error() == 4 ); | |
44 | ||
45 | std::unexpected<X> u6(std::move(u4)); | |
46 | VERIFY( u6.error() == 4 ); | |
47 | VERIFY( u4.error() == -1 ); | |
48 | ||
49 | u6 = u3; | |
50 | VERIFY( u6.error() == 3 ); | |
51 | VERIFY( u3.error() == 3 ); | |
52 | ||
53 | u5 = std::move(u3); | |
54 | VERIFY( u5.error() == 3 ); | |
55 | VERIFY( u3.error() == -1 ); | |
56 | ||
57 | u5.swap(u3); | |
58 | VERIFY( u3.error() == 3 ); | |
59 | VERIFY( u5.error() == -1 ); | |
60 | ||
61 | swap(u5, u3); | |
62 | VERIFY( u5.error() == 3 ); | |
63 | VERIFY( u3.error() == -1 ); | |
64 | ||
65 | VERIFY( u1 == u1 ); | |
66 | VERIFY( u1 != u2 ); | |
67 | VERIFY( u3 == u4 ); | |
68 | ||
69 | // CTAD | |
70 | std::unexpected u7(1L); | |
71 | static_assert( std::is_same_v<decltype(u7), std::unexpected<long>> ); | |
72 | ||
73 | return true; | |
74 | } | |
75 | ||
59822c39 JW |
76 | static_assert( std::is_swappable_v<std::unexpected<int>> ); |
77 | struct A { A& operator=(A&&) = delete; }; | |
78 | static_assert( ! std::is_swappable_v<std::unexpected<A>> ); | |
79 | ||
b78e0ce2 JW |
80 | int main() |
81 | { | |
82 | static_assert( test() ); | |
83 | test(); | |
84 | } |