Debian Codegraph @

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

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