1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
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&&;
21 #include "test_macros.h"
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;
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;
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;
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; }
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;
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;
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;
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; }
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;
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;
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;
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
; }
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;
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;
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
; }
137 NoCopy(const NoCopy
&) { assert(false); }
138 std::optional
<int> operator()(const NoCopy
&&) { return 1; }
142 std::optional
<int> non_const() { return 1; }
145 constexpr void test_val_types() {
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};
160 assert(i
.and_then(l
) == 1);
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};
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>);
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(); };
234 std::move(opt
).and_then(l
);
237 constexpr bool test() {
239 std::optional
<int> opt
{};
240 const auto& copt
= opt
;
242 const auto never_called
= [](int) {
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
{});
260 int main(int, char**) {
262 static_assert(test());