Debian Codegraph @

libstdc++-v3/testsuite/20_util/optional/monadic/transform.cc

raw
1 // { dg-do run { target c++23 } }
2
3 #include <optional>
4 #include <testsuite_hooks.h>
5
6 constexpr bool
7 test_transform()
8 {
9 std::optional<int> o;
10 auto&& r = o.transform([](int) -> unsigned { throw 1; });
11 static_assert( std::is_same_v<decltype(r), std::optional<unsigned>&&> );
12 VERIFY( ! r.has_value() );
13
14 o = 10;
15 auto&& r2 = o.transform([](int i) -> unsigned { return i + 2u; });
16 static_assert( std::is_same_v<decltype(r2), std::optional<unsigned>&&> );
17 VERIFY( *r2 == 12u );
18
19 return true;
20 }
21
22 static_assert( test_transform() );
23
24 enum { CalledLvalue = 1, CalledConst = 2, PassedLvalue = 4, PassedConst = 8 };
25
26 struct F
27 {
28 template<typename This, typename Value>
29 static constexpr int
30 called_as()
31 {
32 int res = 0;
33 if constexpr (std::is_lvalue_reference_v<This>)
34 res |= CalledLvalue;
35 if constexpr (std::is_const_v<std::remove_reference_t<This>>)
36 res |= CalledConst;
37
38 if constexpr (std::is_lvalue_reference_v<Value>)
39 res |= PassedLvalue;
40 if constexpr (std::is_const_v<std::remove_reference_t<Value>>)
41 res |= PassedConst;
42
43 return res;
44 }
45
46 template<typename T>
47 constexpr int
48 operator()(T&&) &
49 { return called_as<F&, T>(); }
50
51 template<typename T>
52 constexpr int
53 operator()(T&&) const &
54 { return called_as<const F&, T>(); }
55
56 template<typename T>
57 constexpr int
58 operator()(T&&) &&
59 { return called_as<F, T>(); }
60
61 template<typename T>
62 constexpr int
63 operator()(T&&) const &&
64 { return called_as<const F, T>(); }
65 };
66
67 constexpr bool
68 test_forwarding()
69 {
70 std::optional<long> o = 1;
71 F f;
72
73 VERIFY( *o.transform(f) == (PassedLvalue|CalledLvalue) );
74 VERIFY( *o.transform(std::move(f)) == PassedLvalue );
75 VERIFY( *std::move(o).transform(f) == CalledLvalue );
76 VERIFY( *std::move(o).transform(std::move(f)) == 0 );
77
78 const auto& co = o;
79
80 VERIFY( *co.transform(f) == (PassedLvalue|PassedConst|CalledLvalue) );
81 VERIFY( *co.transform(std::move(f)) == (PassedLvalue|PassedConst) );
82 VERIFY( *std::move(co).transform(f) == (PassedConst|CalledLvalue) );
83 VERIFY( *std::move(co).transform(std::move(f)) == PassedConst );
84
85 const auto& cf = f;
86
87 VERIFY( *o.transform(cf) == (PassedLvalue|CalledLvalue|CalledConst) );
88 VERIFY( *o.transform(std::move(cf)) == (PassedLvalue|CalledConst) );
89 VERIFY( *std::move(o).transform(cf) == (CalledLvalue|CalledConst) );
90 VERIFY( *std::move(o).transform(std::move(cf)) == CalledConst );
91
92 VERIFY( *co.transform(cf) == (PassedLvalue|PassedConst|CalledLvalue|CalledConst) );
93 VERIFY( *co.transform(std::move(cf)) == (PassedLvalue|PassedConst|CalledConst) );
94 VERIFY( *std::move(co).transform(cf) == (PassedConst|CalledLvalue|CalledConst) );
95 VERIFY( *std::move(co).transform(std::move(cf)) == (PassedConst|CalledConst) );
96
97 o = std::nullopt;
98
99 VERIFY( ! o.transform(f).has_value() );
100 VERIFY( ! co.transform(f).has_value() );
101 VERIFY( ! std::move(o).transform(f).has_value() );
102 VERIFY( ! std::move(co).transform(f).has_value() );
103
104 return true;
105 }
106
107 static_assert( test_forwarding() );
108
109 constexpr bool
110 test_copy_elision()
111 {
112 struct immovable
113 {
114 constexpr immovable(int p) : power_level(p) { }
115 immovable(immovable&&) = delete;
116
117 int power_level;
118 };
119
120 struct Force
121 {
122 constexpr immovable operator()(int i) const { return {i+1}; }
123 };
124
125 std::optional<int> irresistible(9000);
126 std::optional<immovable> object = irresistible.transform(Force{});
127 VERIFY( object->power_level > 9000 );
128
129 return true;
130 }
131
132 static_assert( test_copy_elision() );
133
134 void f(int&) { }
135
136 void
137 test_unconstrained()
138 {
139 // PR libstdc++/102863 - Optional monadic ops should not be constrained
140 std::optional<int> x;
141 auto answer = x.transform([](auto& y) { f(y); return 42; });
142 VERIFY( !answer );
143 }
144
145 int main()
146 {
147 test_transform();
148 test_forwarding();
149 test_copy_elision();
150 test_unconstrained();
151 }