Debian Codegraph @

libstdc++-v3/testsuite/20_util/expected/requirements.cc

raw
1 // { dg-do compile { target c++23 } }
2
3 #include <expected>
4 #include <type_traits>
5
6 // Default construction
7
8 template<typename T, typename E>
9 constexpr bool default_constructible
10 = std::is_default_constructible_v<std::expected<T, E>>;
11
12 struct A { A(int); };
13
14 static_assert( default_constructible< int, int > );
15 static_assert( default_constructible< A, int > == false );
16 static_assert( default_constructible< int, A > );
17 static_assert( default_constructible< A, A > == false );
18 static_assert( default_constructible< int, A > );
19 static_assert( default_constructible< void, int > );
20
21 // Destruction
22
23 template<typename T, typename E>
24 constexpr bool trivially_destructible
25 = std::is_trivially_destructible_v<std::expected<T, E>>;
26
27 struct B { ~B(); };
28
29 static_assert( trivially_destructible< int, int > );
30 static_assert( trivially_destructible< B, int > == false );
31 static_assert( trivially_destructible< int, B > == false );
32 static_assert( trivially_destructible< B, B > == false );
33 static_assert( trivially_destructible< void, int > );
34 static_assert( trivially_destructible< void, B > == false );
35
36 enum Result { No, Yes, NoThrow, Trivial };
37
38 // Copy construction
39
40 template<typename T, typename E>
41 constexpr Result copy_constructible
42 = std::is_trivially_copy_constructible_v<std::expected<T, E>> ? Trivial
43 : std::is_copy_constructible_v<std::expected<T, E>> ? Yes
44 : No;
45
46 struct C { C(const C&); };
47 struct D { D(D&&); };
48
49 static_assert( copy_constructible< int, int > == Trivial );
50 static_assert( copy_constructible< C, C > == Yes );
51 static_assert( copy_constructible< C, int > == Yes );
52 static_assert( copy_constructible< int, C > == Yes );
53 static_assert( copy_constructible< int, D > == No );
54 static_assert( copy_constructible< D, int > == No );
55 static_assert( copy_constructible< D, D > == No );
56 static_assert( copy_constructible< void, int > == Trivial );
57 static_assert( copy_constructible< void, C > == Yes );
58 static_assert( copy_constructible< void, D > == No );
59
60 // Move construction
61
62 template<typename T, typename E>
63 constexpr Result move_constructible
64 = std::is_trivially_move_constructible_v<std::expected<T, E>> ? Trivial
65 : std::is_nothrow_move_constructible_v<std::expected<T, E>> ? NoThrow
66 : std::is_move_constructible_v<std::expected<T, E>> ? Yes
67 : No;
68
69 struct E { E(E&&) noexcept; };
70
71 static_assert( move_constructible< int, int > == Trivial );
72 static_assert( move_constructible< C, C > == Yes );
73 static_assert( move_constructible< C, int > == Yes );
74 static_assert( move_constructible< int, C > == Yes );
75 static_assert( move_constructible< D, D > == Yes );
76 static_assert( move_constructible< D, int > == Yes );
77 static_assert( move_constructible< int, D > == Yes );
78 static_assert( move_constructible< E, E > == NoThrow );
79 static_assert( move_constructible< E, int > == NoThrow );
80 static_assert( move_constructible< int, E > == NoThrow );
81 static_assert( move_constructible< void, int > == Trivial );
82 static_assert( move_constructible< void, C > == Yes );
83 static_assert( move_constructible< void, D > == Yes );
84 static_assert( move_constructible< void, E > == NoThrow );
85
86 // Copy assignment
87
88 template<typename T, typename E>
89 constexpr bool copy_assignable
90 = std::is_copy_assignable_v<std::expected<T, E>>;
91
92 struct F { F(F&&); F& operator=(const F&); }; // not copy-constructible
93 struct G { G(const G&); G(G&&); G& operator=(const G&); }; // throwing move
94
95 static_assert( copy_assignable< int, int > );
96 static_assert( copy_assignable< F, int > == false );
97 static_assert( copy_assignable< int, F > == false );
98 static_assert( copy_assignable< F, F > == false );
99 static_assert( copy_assignable< G, int > );
100 static_assert( copy_assignable< int, G > );
101 static_assert( copy_assignable< G, G > == false );
102 static_assert( copy_assignable< void, int > );
103 static_assert( copy_assignable< void, F > == false );
104 static_assert( copy_assignable< void, G > );
105
106 // Move assignment
107
108 template<typename T, typename E>
109 constexpr bool move_assignable
110 = std::is_move_assignable_v<std::expected<T, E>>;
111
112 static_assert( move_assignable< int, int > );
113 static_assert( move_assignable< F, int > );
114 static_assert( move_assignable< int, F > );
115 static_assert( move_assignable< F, F > == false );
116 static_assert( move_assignable< G, int > );
117 static_assert( move_assignable< int, G > );
118 static_assert( move_assignable< G, G > == false );
119 static_assert( move_assignable< void, int > );
120 static_assert( move_assignable< void, F > );
121 static_assert( move_assignable< void, G > );
122
123 // QoI properties
124 static_assert( sizeof(std::expected<char, unsigned char>) == 2 );
125 static_assert( sizeof(std::expected<void, char>) == 2 );
126 static_assert( sizeof(std::expected<void*, char>) == sizeof(void*) + __alignof(void*) );
127 static_assert( alignof(std::expected<void, char>) == 1 );
128 static_assert( alignof(std::expected<void*, char>) == alignof(void*) );