Skip to content

Commit c12481f

Browse files
committed
Gate ref tests, use std::same_as suggestion
1 parent afa1c79 commit c12481f

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

libcxx/test/std/utilities/optional/optional.monadic/and_then.pass.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// template<class F> constexpr auto and_then(F&&) const&&;
1717

1818
#include <cassert>
19+
#include <concepts>
1920
#include <optional>
2021

2122
#include "test_macros.h"
@@ -265,20 +266,22 @@ constexpr bool test_ref() {
265266
{
266267
int j = 42;
267268
std::optional<int&> i{j};
268-
assert(i.and_then(LVal{}) == 1);
269+
std::same_as<std::optional<int>> decltype(auto) r = i.and_then(LVal{});
270+
271+
assert(r == 1);
269272
assert(i.and_then(NOLVal{}) == std::nullopt);
270-
ASSERT_SAME_TYPE(decltype(i.and_then(LVal{})), std::optional<int>);
271273
}
272274

273275
//With & qualifier on F's operator()
274276
{
275277
int j = 42;
276278
std::optional<int&> i{j};
277279
RefQual l{};
278-
assert(i.and_then(l) == 1);
279280
NORefQual nl{};
281+
std::same_as<std::optional<int>> decltype(auto) r = i.and_then(l);
282+
283+
assert(r == 1);
280284
assert(i.and_then(nl) == std::nullopt);
281-
ASSERT_SAME_TYPE(decltype(i.and_then(l)), std::optional<int>);
282285
}
283286
}
284287

@@ -288,20 +291,22 @@ constexpr bool test_ref() {
288291
{
289292
int j = 42;
290293
std::optional<const int&> i{j};
291-
assert(i.and_then(CLVal{}) == 1);
294+
std::same_as<std::optional<int>> decltype(auto) r = i.and_then(CLVal{});
295+
296+
assert(r == 1);
292297
assert(i.and_then(NOCLVal{}) == std::nullopt);
293-
ASSERT_SAME_TYPE(decltype(i.and_then(CLVal{})), std::optional<int>);
294298
}
295299

296300
//With & qualifier on F's operator()
297301
{
298302
int j = 42;
299303
const std::optional<int&> i{j};
300304
const CRefQual l{};
301-
assert(i.and_then(l) == 1);
302305
const NOCRefQual nl{};
306+
std::same_as<std::optional<int>> decltype(auto) r = i.and_then(l);
307+
308+
assert(r == 1);
303309
assert(i.and_then(nl) == std::nullopt);
304-
ASSERT_SAME_TYPE(decltype(i.and_then(l)), std::optional<int>);
305310
}
306311
}
307312
// Test && overload
@@ -310,9 +315,10 @@ constexpr bool test_ref() {
310315
{
311316
int j = 42;
312317
std::optional<int&> i{j};
313-
assert(i.and_then(RVRefQual{}) == 1);
318+
std::same_as<std::optional<int>> decltype(auto) r = i.and_then(RVRefQual{});
319+
320+
assert(r == 1);
314321
assert(i.and_then(NORVRefQual{}) == std::nullopt);
315-
ASSERT_SAME_TYPE(decltype(i.and_then(RVRefQual{})), std::optional<int>);
316322
}
317323
}
318324

@@ -323,10 +329,11 @@ constexpr bool test_ref() {
323329
int j = 42;
324330
const std::optional<int&> i{j};
325331
const RVCRefQual l{};
326-
assert(i.and_then(std::move(l)) == 1);
327332
const NORVCRefQual nl{};
333+
std::same_as<std::optional<int>> decltype(auto) r = i.and_then(std::move(l));
334+
335+
assert(r == 1);
328336
assert(i.and_then(std::move(nl)) == std::nullopt);
329-
ASSERT_SAME_TYPE(decltype(i.and_then(std::move(l))), std::optional<int>);
330337
}
331338
}
332339
return true;
@@ -336,7 +343,9 @@ constexpr bool test_ref() {
336343
int main(int, char**) {
337344
test();
338345
static_assert(test());
346+
#if TEST_STD_VER >= 26
339347
test_ref();
340348
static_assert(test_ref());
349+
#endif
341350
return 0;
342351
}

libcxx/test/std/utilities/optional/optional.monadic/transform.pass.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "test_macros.h"
1919
#include <cassert>
20+
#include <concepts>
2021
#include <optional>
2122
#include <type_traits>
2223
#include <utility>
@@ -201,40 +202,41 @@ constexpr bool test() {
201202
return true;
202203
}
203204

205+
#if TEST_STD_VER >= 26
204206
constexpr bool test_ref() {
205207
{
206208
std::optional<int&> opt1;
207-
auto opt1r = opt1.transform([](int i) { return i + 2; });
209+
std::same_as<std::optional<int>> decltype(auto) opt1r = opt1.transform([](int i) { return i + 2; });
208210
assert(!opt1);
209-
ASSERT_SAME_TYPE(decltype(opt1r), std::optional<int>);
211+
assert(!opt1r);
210212
}
211213

212214
{
213215
int i = 42;
214216
std::optional<int&> opt{i};
215-
auto o2 = opt.transform([](int j) { return j + 2; });
217+
std::same_as<std::optional<int>> decltype(auto) o2 = opt.transform([](int i) { return i + 2; });
218+
216219
assert(*o2 == 44);
217-
ASSERT_SAME_TYPE(decltype(o2), std::optional<int>);
218220
}
219221
// Test & overload
220222
{
221223
// Without & qualifier on F's operator()
222224
{
223225
int i = 42;
224226
std::optional<int&> opt{i};
225-
auto o3 = opt.transform(LVal{});
227+
std::same_as<std::optional<int>> decltype(auto) o3 = opt.transform(LVal{});
228+
226229
assert(*o3 == 1);
227-
ASSERT_SAME_TYPE(decltype(o3), std::optional<int>);
228230
}
229231

230232
//With & qualifier on F's operator()
231233
{
232234
int i = 42;
233235
std::optional<int&> opt{i};
234236
RefQual l{};
235-
auto o3 = opt.transform(l);
237+
std::same_as<std::optional<int>> decltype(auto) o3 = opt.transform(l);
238+
236239
assert(*o3 == 1);
237-
ASSERT_SAME_TYPE(decltype(o3), std::optional<int>);
238240
}
239241
}
240242
// const& overload
@@ -243,19 +245,19 @@ constexpr bool test_ref() {
243245
{
244246
int i = 42;
245247
std::optional<const int&> opt{i};
246-
auto o3 = std::as_const(opt).transform(CLVal{});
248+
std::same_as<std::optional<int>> decltype(auto) o3 = std::as_const(opt).transform(CLVal{});
249+
247250
assert(*o3 == 1);
248-
ASSERT_SAME_TYPE(decltype(o3), std::optional<int>);
249251
}
250252

251253
//With & qualifier on F's operator()
252254
{
253255
int i = 42;
254256
const std::optional<int&> opt{i};
255257
const CRefQual l{};
256-
auto o3 = opt.transform(l);
258+
std::same_as<std::optional<int>> decltype(auto) o3 = opt.transform(l);
259+
257260
assert(*o3 == 1);
258-
ASSERT_SAME_TYPE(decltype(o3), std::optional<int>);
259261
}
260262
}
261263

@@ -265,18 +267,17 @@ constexpr bool test_ref() {
265267
{
266268
int i = 42;
267269
std::optional<int> opt{i};
268-
auto o3 = std::move(opt).transform(RVal{});
270+
std::same_as<std::optional<int>> decltype(auto) o3 = std::move(opt).transform(RVal{});
271+
269272
assert(*o3 == 1);
270-
ASSERT_SAME_TYPE(decltype(o3), std::optional<int>);
271273
}
272274

273275
//With & qualifier on F's operator()
274276
{
275277
int i = 42;
276278
std::optional<int&> opt{i};
277-
auto o3 = std::move(opt).transform(RVRefQual{});
279+
std::same_as<std::optional<int>> decltype(auto) o3 = std::move(opt).transform(RVRefQual{});
278280
assert(*o3 == 1);
279-
ASSERT_SAME_TYPE(decltype(o3), std::optional<int>);
280281
}
281282
}
282283

@@ -287,9 +288,8 @@ constexpr bool test_ref() {
287288
int i = 42;
288289
std::optional<int&> opt{i};
289290
const RVCRefQual rvc{};
290-
auto o3 = opt.transform(std::move(rvc));
291+
std::same_as<std::optional<int>> decltype(auto) o3 = opt.transform(std::move(rvc));
291292
assert(*o3 == 1);
292-
ASSERT_SAME_TYPE(decltype(o3), std::optional<int>);
293293
}
294294
}
295295
{
@@ -299,11 +299,14 @@ constexpr bool test_ref() {
299299
}
300300
return true;
301301
}
302+
#endif
302303

303304
int main(int, char**) {
304305
test();
305306
static_assert(test());
307+
#if TEST_STD_VER >= 26
306308
test_ref();
307309
static_assert(test_ref());
310+
#endif
308311
return 0;
309312
}

0 commit comments

Comments
 (0)