| // { dg-do compile { target c++26 } } | |
| #include <ranges> | |
| #include <algorithm> | |
| #include <string_view> | |
| #include <type_traits> | |
| #include <tuple> | |
| #include <utility> | |
| #include <testsuite_hooks.h> | |
| template <typename It, typename St> | |
| constexpr void | |
| test_sanity() | |
| { | |
| using R = std::ranges::subrange<It, St>; | |
| static_assert(std::tuple_size_v<R> == 2); | |
| static_assert(std::is_same_v<std::tuple_element_t<0, R>, It>); | |
| static_assert(std::is_same_v<std::tuple_element_t<1, R>, St>); | |
| static_assert(std::is_same_v<decltype(std::get<0>(std::declval<R>())), It>); | |
| if constexpr (std::copyable<It>) | |
| { | |
| static_assert(std::is_same_v<decltype(std::get<0>(std::declval<R&>())), It>); | |
| static_assert(std::is_same_v<decltype(std::get<0>(std::declval<const R>())), It>); | |
| static_assert(std::is_same_v<decltype(std::get<0>(std::declval<const R&>())), It>); | |
| } | |
| static_assert(std::is_same_v<decltype(std::get<1>(std::declval<R&>())), St>); | |
| static_assert(std::is_same_v<decltype(std::get<1>(std::declval<R>())), St>); | |
| static_assert(std::is_same_v<decltype(std::get<1>(std::declval<const R>())), St>); | |
| static_assert(std::is_same_v<decltype(std::get<1>(std::declval<const R&>())), St>); | |
| } | |
| template <typename It, typename St> | |
| constexpr void | |
| test_get() | |
| { | |
| using R = std::ranges::subrange<It, St>; | |
| R rng{It(), St()}; | |
| if constexpr (std::copyable<It>) | |
| { | |
| const R crng{It(), St()}; | |
| VERIFY(std::get<0>(rng) == std::get<1>(rng)); | |
| VERIFY(std::get<0>(crng) == std::get<1>(crng)); | |
| } | |
| VERIFY(std::get<0>(std::move(rng)) == std::get<1>(std::move(rng))); | |
| } | |
| template <typename It, typename St> | |
| constexpr void | |
| test_structured_binding() | |
| { | |
| using R = std::ranges::subrange<It, St>; | |
| R rng{It(), St()}; | |
| if constexpr (std::copyable<It>) | |
| { | |
| auto& [b1, e1] = rng; | |
| VERIFY( b1 == e1 ); | |
| const R crng{It(), St()}; | |
| auto& [b2, e2] = crng; | |
| VERIFY( b2 == e2 ); | |
| } | |
| auto [b, e] = std::move(rng); | |
| VERIFY( b == e ); | |
| } | |
| template <typename It, typename St> | |
| constexpr void | |
| test_tuple_cat() | |
| { | |
| std::ranges::subrange<It, St> rng{It(), St()}; | |
| std::pair<int, std::string_view> p(42, "hello"); | |
| auto r = std::tuple_cat(std::move(rng), p); | |
| static_assert(std::is_same_v<decltype(r), std::tuple<It, St, int, std::string_view>>); | |
| VERIFY( std::get<0>(r) == std::get<1>(r) ); | |
| VERIFY( std::get<2>(r) == 42 ); | |
| VERIFY( std::get<3>(r) == "hello" ); | |
| } | |
| template<typename Fn, typename Tuple> | |
| concept can_apply = requires (Fn f, Tuple t) | |
| { std::apply(std::forward<Fn>(f), std::forward<Tuple>(t)); }; | |
| template<typename Dest, typename Tuple> | |
| concept can_make_from_tuple = requires (Tuple t) | |
| { std::make_from_tuple<Dest>(std::forward<Tuple>(t)); }; | |
| template<int V, typename Res, typename Fn, typename Tuple> | |
| constexpr void | |
| check_apply() | |
| { | |
| #if __cpp_lib_apply >= 202506L | |
| constexpr bool callable = (V > 0); | |
| constexpr bool noex = (V > 1); | |
| static_assert(can_apply<Fn, Tuple> == callable); | |
| static_assert(std::is_applicable<Fn, Tuple>::value == callable); | |
| static_assert(std::is_applicable_v<Fn, Tuple> == callable); | |
| static_assert(std::is_nothrow_applicable<Fn, Tuple>::value == noex); | |
| static_assert(std::is_nothrow_applicable_v<Fn, Tuple> == noex); | |
| if constexpr (callable) | |
| { | |
| static_assert(std::is_same_v<Res, typename std::apply_result<Fn, Tuple>::type>); | |
| static_assert(std::is_same_v<Res, std::apply_result_t<Fn, Tuple>>); | |
| } | |
| else | |
| { | |
| static_assert(!requires { typename std::apply_result<Fn, Tuple>::type; }); | |
| static_assert(!requires { typename std::apply_result_t<Fn, Tuple>; }); | |
| } | |
| #else | |
| static_assert(can_apply<F, Tuple>); | |
| #endif | |
| } | |
| struct Cont | |
| { | |
| template<typename T, typename U> | |
| constexpr Cont(const T&, const U&) noexcept | |
| { } | |
| }; | |
| template<typename Rng> | |
| constexpr bool noexcept_get | |
| = noexcept(std::get<0>(std::declval<Rng>())) && noexcept(std::get<1>(std::declval<Rng>())); | |
| template <typename It, typename St> | |
| constexpr void | |
| test_apply() | |
| { | |
| using R = std::ranges::subrange<It, St>; | |
| R rng{It(), St()}; | |
| const R& crng = rng; | |
| auto f = [](It b, St e) noexcept { return b == e; }; | |
| if constexpr (std::copyable<It>) | |
| { | |
| auto r1 = std::apply(f, rng); | |
| VERIFY( r1 ); | |
| static_assert( noexcept(std::apply(f, rng)) == noexcept_get<R&> ); | |
| check_apply<1, bool, decltype((f)), R&>(); | |
| auto r2 = std::apply(f, crng); | |
| VERIFY( r2 ); | |
| static_assert( noexcept(std::apply(f, crng)) == noexcept_get<const R&> ); | |
| check_apply<1, bool, decltype((f)), const R&>(); | |
| } | |
| else | |
| { | |
| check_apply<0, bool, decltype((f)), R&>(); | |
| check_apply<0, bool, decltype((f)), const R&>(); | |
| } | |
| auto g = [](int, int) noexcept { }; | |
| check_apply<0, void, decltype((g)), R>(); | |
| check_apply<0, void, decltype((g)), R&>(); | |
| check_apply<0, void, decltype((g)), const R&>(); | |
| auto r = std::apply(f, std::move(rng)); | |
| VERIFY( r ); | |
| static_assert( noexcept(std::apply(f, std::move(rng))) == noexcept_get<R> ); | |
| check_apply<1, bool, decltype((f)), R>(); | |
| if constexpr (std::copyable<It>) | |
| { | |
| auto p1 = std::make_from_tuple<Cont>(crng); | |
| static_assert( noexcept(std::make_from_tuple<Cont>(rng)) == noexcept_get<R&> ); | |
| auto p2 = std::make_from_tuple<Cont>(crng); | |
| static_assert( noexcept(std::make_from_tuple<Cont>(rng)) == noexcept_get<const R&> ); | |
| } | |
| else | |
| { | |
| static_assert(!can_make_from_tuple<Cont, R&>); | |
| static_assert(!can_make_from_tuple<Cont, const R&>); | |
| } | |
| auto p = std::make_from_tuple<Cont>(std::move(rng)); | |
| static_assert( noexcept(std::make_from_tuple<Cont>(std::move(rng))) == noexcept_get<R> ); | |
| static_assert(!can_make_from_tuple<std::pair<int, int>, R>); | |
| static_assert(!can_make_from_tuple<std::pair<int, int>, R&>); | |
| static_assert(!can_make_from_tuple<std::pair<int, int>, const R&>); | |
| } | |
| template <typename It, typename St> | |
| constexpr bool | |
| test_all() | |
| { | |
| test_sanity<It, St>(); | |
| test_structured_binding<It, St>(); | |
| test_tuple_cat<It, St>(); | |
| test_apply<It, St>(); | |
| test_get<It, St>(); | |
| return true; | |
| } | |
| template<int Move, int Copy> | |
| struct Iterator | |
| { | |
| using iterator_concept = std::input_iterator_tag; | |
| using value_type = int; | |
| using difference_type = std::ptrdiff_t; | |
| constexpr Iterator() = default; | |
| constexpr Iterator(Iterator&&) noexcept(Move > 1) | |
| requires (Move > 0) | |
| { } | |
| constexpr Iterator(const Iterator&) noexcept(Copy > 1) | |
| requires (Copy > 0) | |
| { } | |
| constexpr Iterator& operator=(Iterator&&) noexcept(Move > 1) | |
| requires (Move > 0) | |
| { return *this; } | |
| constexpr Iterator& operator=(const Iterator&) noexcept(Copy > 1) | |
| requires (Copy > 0) | |
| { return *this; } | |
| constexpr int | |
| operator*() const | |
| { return 10; } | |
| constexpr Iterator& | |
| operator++() | |
| { return *this; } | |
| constexpr void | |
| operator++(int) | |
| { } | |
| friend constexpr bool | |
| operator==(const Iterator&, const Iterator&) | |
| { return true; } | |
| friend constexpr bool | |
| operator==(const Iterator&, std::default_sentinel_t) | |
| { return true; } | |
| }; | |
| static_assert( test_all<Iterator<1, 0>, std::default_sentinel_t>() ); | |
| static_assert( test_all<Iterator<2, 0>, std::default_sentinel_t>() ); | |
| static_assert( test_all<Iterator<1, 1>, std::default_sentinel_t>() ); | |
| static_assert( test_all<Iterator<2, 1>, std::default_sentinel_t>() ); | |
| static_assert( test_all<Iterator<1, 1>, Iterator<1, 1>>() ); | |
| static_assert( test_all<Iterator<2, 1>, Iterator<2, 1>>() ); | |
| static_assert( test_all<Iterator<2, 2>, Iterator<2, 2>>() ); | |