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