| 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 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // REQUIRES: std-at-least-c++23 |
| 10 | |
| 11 | // <ranges> |
| 12 | |
| 13 | // join_with_view() |
| 14 | // requires default_initializable<V> && default_initializable<Pattern> = default; |
| 15 | |
| 16 | #include <ranges> |
| 17 | |
| 18 | #include <algorithm> |
| 19 | #include <array> |
| 20 | #include <cassert> |
| 21 | #include <type_traits> |
| 22 | |
| 23 | static constexpr auto view = std::to_array<std::array<int, 2>>({{1, 2}, {3, 4}, {5, 6}}); |
| 24 | |
| 25 | struct TrivialView : std::ranges::view_base { |
| 26 | int val_; // intentionally uninitialized |
| 27 | |
| 28 | constexpr auto begin() { return view.data(); } |
| 29 | constexpr auto end() { return view.data() + view.size(); } |
| 30 | }; |
| 31 | |
| 32 | static_assert(std::is_trivially_copyable_v<TrivialView> && std::is_trivially_default_constructible_v<TrivialView>); |
| 33 | |
| 34 | struct NonDefaultConstructibleView : TrivialView { |
| 35 | NonDefaultConstructibleView(int); |
| 36 | }; |
| 37 | |
| 38 | struct TrivialPattern : std::ranges::view_base { |
| 39 | int val_; // intentionally uninitialized |
| 40 | |
| 41 | constexpr int* begin() { return &val_; } |
| 42 | constexpr int* end() { return &val_ + 1; } |
| 43 | }; |
| 44 | |
| 45 | static_assert(std::is_trivially_copyable_v<TrivialPattern> && |
| 46 | std::is_trivially_default_constructible_v<TrivialPattern>); |
| 47 | |
| 48 | struct NonDefaultConstructiblePattern : TrivialPattern { |
| 49 | NonDefaultConstructiblePattern(int); |
| 50 | }; |
| 51 | |
| 52 | constexpr bool test() { |
| 53 | { // Check if `base_` and `pattern_` are value initialised |
| 54 | std::ranges::join_with_view<TrivialView, TrivialPattern> v; |
| 55 | assert(std::move(v).base().val_ == 0); |
| 56 | assert(std::ranges::equal(v, std::array{1, 2, 0, 3, 4, 0, 5, 6})); |
| 57 | } |
| 58 | |
| 59 | { // Default constructor should not be explicit |
| 60 | [[maybe_unused]] std::ranges::join_with_view<TrivialView, TrivialPattern> v = {}; |
| 61 | } |
| 62 | |
| 63 | static_assert(std::default_initializable<std::ranges::join_with_view<TrivialView, TrivialPattern>>); |
| 64 | static_assert(!std::default_initializable<std::ranges::join_with_view<TrivialView, NonDefaultConstructiblePattern>>); |
| 65 | static_assert(!std::default_initializable<std::ranges::join_with_view<NonDefaultConstructibleView, TrivialPattern>>); |
| 66 | static_assert(!std::default_initializable< |
| 67 | std::ranges::join_with_view<NonDefaultConstructibleView, NonDefaultConstructiblePattern>>); |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | int main(int, char**) { |
| 73 | test(); |
| 74 | static_assert(test()); |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |