Revert "[ELF] Refine isExported/isPreemptible condition"
[llvm-project.git] / libcxx / test / std / utilities / optional / optional.monadic / and_then.pass.cpp
blob97305d976e0664a5b6c5e946fd1323490749aca9
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
11 // <optional>
13 // template<class F> constexpr auto and_then(F&&) &;
14 // template<class F> constexpr auto and_then(F&&) &&;
15 // template<class F> constexpr auto and_then(F&&) const&;
16 // template<class F> constexpr auto and_then(F&&) const&&;
18 #include <cassert>
19 #include <optional>
21 #include "test_macros.h"
23 struct LVal {
24 constexpr std::optional<int> operator()(int&) { return 1; }
25 std::optional<int> operator()(const int&) = delete;
26 std::optional<int> operator()(int&&) = delete;
27 std::optional<int> operator()(const int&&) = delete;
30 struct CLVal {
31 std::optional<int> operator()(int&) = delete;
32 constexpr std::optional<int> operator()(const int&) { return 1; }
33 std::optional<int> operator()(int&&) = delete;
34 std::optional<int> operator()(const int&&) = delete;
37 struct RVal {
38 std::optional<int> operator()(int&) = delete;
39 std::optional<int> operator()(const int&) = delete;
40 constexpr std::optional<int> operator()(int&&) { return 1; }
41 std::optional<int> operator()(const int&&) = delete;
44 struct CRVal {
45 std::optional<int> operator()(int&) = delete;
46 std::optional<int> operator()(const int&) = delete;
47 std::optional<int> operator()(int&&) = delete;
48 constexpr std::optional<int> operator()(const int&&) { return 1; }
51 struct RefQual {
52 constexpr std::optional<int> operator()(int) & { return 1; }
53 std::optional<int> operator()(int) const& = delete;
54 std::optional<int> operator()(int) && = delete;
55 std::optional<int> operator()(int) const&& = delete;
58 struct CRefQual {
59 std::optional<int> operator()(int) & = delete;
60 constexpr std::optional<int> operator()(int) const& { return 1; }
61 std::optional<int> operator()(int) && = delete;
62 std::optional<int> operator()(int) const&& = delete;
65 struct RVRefQual {
66 std::optional<int> operator()(int) & = delete;
67 std::optional<int> operator()(int) const& = delete;
68 constexpr std::optional<int> operator()(int) && { return 1; }
69 std::optional<int> operator()(int) const&& = delete;
72 struct RVCRefQual {
73 std::optional<int> operator()(int) & = delete;
74 std::optional<int> operator()(int) const& = delete;
75 std::optional<int> operator()(int) && = delete;
76 constexpr std::optional<int> operator()(int) const&& { return 1; }
79 struct NOLVal {
80 constexpr std::optional<int> operator()(int&) { return std::nullopt; }
81 std::optional<int> operator()(const int&) = delete;
82 std::optional<int> operator()(int&&) = delete;
83 std::optional<int> operator()(const int&&) = delete;
86 struct NOCLVal {
87 std::optional<int> operator()(int&) = delete;
88 constexpr std::optional<int> operator()(const int&) { return std::nullopt; }
89 std::optional<int> operator()(int&&) = delete;
90 std::optional<int> operator()(const int&&) = delete;
93 struct NORVal {
94 std::optional<int> operator()(int&) = delete;
95 std::optional<int> operator()(const int&) = delete;
96 constexpr std::optional<int> operator()(int&&) { return std::nullopt; }
97 std::optional<int> operator()(const int&&) = delete;
100 struct NOCRVal {
101 std::optional<int> operator()(int&) = delete;
102 std::optional<int> operator()(const int&) = delete;
103 std::optional<int> operator()(int&&) = delete;
104 constexpr std::optional<int> operator()(const int&&) { return std::nullopt; }
107 struct NORefQual {
108 constexpr std::optional<int> operator()(int) & { return std::nullopt; }
109 std::optional<int> operator()(int) const& = delete;
110 std::optional<int> operator()(int) && = delete;
111 std::optional<int> operator()(int) const&& = delete;
114 struct NOCRefQual {
115 std::optional<int> operator()(int) & = delete;
116 constexpr std::optional<int> operator()(int) const& { return std::nullopt; }
117 std::optional<int> operator()(int) && = delete;
118 std::optional<int> operator()(int) const&& = delete;
121 struct NORVRefQual {
122 std::optional<int> operator()(int) & = delete;
123 std::optional<int> operator()(int) const& = delete;
124 constexpr std::optional<int> operator()(int) && { return std::nullopt; }
125 std::optional<int> operator()(int) const&& = delete;
128 struct NORVCRefQual {
129 std::optional<int> operator()(int) & = delete;
130 std::optional<int> operator()(int) const& = delete;
131 std::optional<int> operator()(int) && = delete;
132 constexpr std::optional<int> operator()(int) const&& { return std::nullopt; }
135 struct NoCopy {
136 NoCopy() = default;
137 NoCopy(const NoCopy&) { assert(false); }
138 std::optional<int> operator()(const NoCopy&&) { return 1; }
141 struct NonConst {
142 std::optional<int> non_const() { return 1; }
145 constexpr void test_val_types() {
146 // Test & overload
148 // Without & qualifier on F's operator()
150 std::optional<int> i{0};
151 assert(i.and_then(LVal{}) == 1);
152 assert(i.and_then(NOLVal{}) == std::nullopt);
153 ASSERT_SAME_TYPE(decltype(i.and_then(LVal{})), std::optional<int>);
156 //With & qualifier on F's operator()
158 std::optional<int> i{0};
159 RefQual l{};
160 assert(i.and_then(l) == 1);
161 NORefQual nl{};
162 assert(i.and_then(nl) == std::nullopt);
163 ASSERT_SAME_TYPE(decltype(i.and_then(l)), std::optional<int>);
167 // Test const& overload
169 // Without & qualifier on F's operator()
171 const std::optional<int> i{0};
172 assert(i.and_then(CLVal{}) == 1);
173 assert(i.and_then(NOCLVal{}) == std::nullopt);
174 ASSERT_SAME_TYPE(decltype(i.and_then(CLVal{})), std::optional<int>);
177 //With & qualifier on F's operator()
179 const std::optional<int> i{0};
180 const CRefQual l{};
181 assert(i.and_then(l) == 1);
182 const NOCRefQual nl{};
183 assert(i.and_then(nl) == std::nullopt);
184 ASSERT_SAME_TYPE(decltype(i.and_then(l)), std::optional<int>);
188 // Test && overload
190 // Without & qualifier on F's operator()
192 std::optional<int> i{0};
193 assert(std::move(i).and_then(RVal{}) == 1);
194 assert(std::move(i).and_then(NORVal{}) == std::nullopt);
195 ASSERT_SAME_TYPE(decltype(std::move(i).and_then(RVal{})), std::optional<int>);
198 //With & qualifier on F's operator()
200 std::optional<int> i{0};
201 assert(i.and_then(RVRefQual{}) == 1);
202 assert(i.and_then(NORVRefQual{}) == std::nullopt);
203 ASSERT_SAME_TYPE(decltype(i.and_then(RVRefQual{})), std::optional<int>);
207 // Test const&& overload
209 // Without & qualifier on F's operator()
211 const std::optional<int> i{0};
212 assert(std::move(i).and_then(CRVal{}) == 1);
213 assert(std::move(i).and_then(NOCRVal{}) == std::nullopt);
214 ASSERT_SAME_TYPE(decltype(std::move(i).and_then(CRVal{})), std::optional<int>);
217 //With & qualifier on F's operator()
219 const std::optional<int> i{0};
220 const RVCRefQual l{};
221 assert(i.and_then(std::move(l)) == 1);
222 const NORVCRefQual nl{};
223 assert(i.and_then(std::move(nl)) == std::nullopt);
224 ASSERT_SAME_TYPE(decltype(i.and_then(std::move(l))), std::optional<int>);
229 // check that the lambda body is not instantiated during overload resolution
230 constexpr void test_sfinae() {
231 std::optional<NonConst> opt{};
232 auto l = [](auto&& x) { return x.non_const(); };
233 opt.and_then(l);
234 std::move(opt).and_then(l);
237 constexpr bool test() {
238 test_val_types();
239 std::optional<int> opt{};
240 const auto& copt = opt;
242 const auto never_called = [](int) {
243 assert(false);
244 return std::optional<int>{};
247 opt.and_then(never_called);
248 std::move(opt).and_then(never_called);
249 copt.and_then(never_called);
250 std::move(copt).and_then(never_called);
252 std::optional<NoCopy> nc;
253 const auto& cnc = nc;
254 std::move(cnc).and_then(NoCopy{});
255 std::move(nc).and_then(NoCopy{});
257 return true;
260 int main(int, char**) {
261 test();
262 static_assert(test());
263 return 0;