| 1 | // { dg-do run { target c++23 } } |
| 2 | // { dg-add-options no_pch } |
| 3 | |
| 4 | #include <ranges> |
| 5 | |
| 6 | #if __cpp_lib_ranges_as_rvalue != 202207L |
| 7 | # error "Feature-test macro __cpp_lib_ranges_as_rvalue has wrong value in <ranges>" |
| 8 | #endif |
| 9 | |
| 10 | #include <algorithm> |
| 11 | #include <memory> |
| 12 | #include <testsuite_hooks.h> |
| 13 | #include <testsuite_iterators.h> |
| 14 | |
| 15 | namespace ranges = std::ranges; |
| 16 | namespace views = std::views; |
| 17 | |
| 18 | |
| 19 | /* Replacement for the standard version, as it's not available in freestanding |
| 20 | environments. */ |
| 21 | template<typename T, typename... Args> |
| 22 | requires (!std::is_array_v<T>) |
| 23 | constexpr auto |
| 24 | make_unique (Args &&...args) |
| 25 | { |
| 26 | return std::unique_ptr<T> { new T (std::forward<Args>(args)...) }; |
| 27 | } |
| 28 | |
| 29 | constexpr bool |
| 30 | test01() |
| 31 | { |
| 32 | |
| 33 | std::unique_ptr<int> a[3] = { make_unique<int>(1), |
| 34 | make_unique<int>(2), |
| 35 | make_unique<int>(3) }; |
| 36 | std::unique_ptr<int> b[3]; |
| 37 | auto v = a | views::as_rvalue; |
| 38 | ranges::copy(v, b); |
| 39 | VERIFY( ranges::all_of(a, [](auto& p) { return p.get() == nullptr; }) ); |
| 40 | VERIFY( ranges::equal(b | views::transform([](auto& p) { return *p; }), (int[]){1, 2, 3}) ); |
| 41 | |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | test02() |
| 47 | { |
| 48 | std::unique_ptr<int> x = make_unique<int>(42); |
| 49 | std::unique_ptr<int> y; |
| 50 | __gnu_test::test_input_range rx(&x, &x+1); |
| 51 | auto v = rx | views::as_rvalue; |
| 52 | static_assert(!ranges::common_range<decltype(v)>); |
| 53 | ranges::copy(v, &y); |
| 54 | VERIFY( x.get() == nullptr ); |
| 55 | VERIFY( *y == 42 ); |
| 56 | } |
| 57 | |
| 58 | int |
| 59 | main() |
| 60 | { |
| 61 | static_assert(test01()); |
| 62 | test02(); |
| 63 | } |