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 // UNSUPPORTED: c++03, c++11, c++14, c++17 10 // UNSUPPORTED: libcpp-has-no-incomplete-ranges 11 12 // class std::ranges::subrange; 13 14 #include <ranges> 15 16 #include <cassert> 17 #include "test_macros.h" 18 #include "test_iterators.h" 19 20 template<size_t I, class S> 21 concept HasGet = requires { 22 std::get<I>(std::declval<S>()); 23 }; 24 25 static_assert( HasGet<0, std::ranges::subrange<int*>>); 26 static_assert( HasGet<1, std::ranges::subrange<int*>>); 27 static_assert(!HasGet<2, std::ranges::subrange<int*>>); 28 static_assert(!HasGet<3, std::ranges::subrange<int*>>); 29 30 constexpr bool test() { 31 { 32 using It = int*; 33 using Sent = sentinel_wrapper<int*>; 34 int a[] = {1, 2, 3}; 35 using R = std::ranges::subrange<It, Sent, std::ranges::subrange_kind::unsized>; 36 R r = R(It(a), Sent(It(a + 3))); 37 ASSERT_SAME_TYPE(decltype(std::get<0>(r)), It); 38 ASSERT_SAME_TYPE(decltype(std::get<1>(r)), Sent); 39 ASSERT_SAME_TYPE(decltype(std::get<0>(static_cast<R&&>(r))), It); 40 ASSERT_SAME_TYPE(decltype(std::get<1>(static_cast<R&&>(r))), Sent); 41 ASSERT_SAME_TYPE(decltype(std::get<0>(static_cast<const R&>(r))), It); 42 ASSERT_SAME_TYPE(decltype(std::get<1>(static_cast<const R&>(r))), Sent); 43 ASSERT_SAME_TYPE(decltype(std::get<0>(static_cast<const R&&>(r))), It); 44 ASSERT_SAME_TYPE(decltype(std::get<1>(static_cast<const R&&>(r))), Sent); 45 assert(base(std::get<0>(r)) == a); // copy from It 46 assert(base(base(std::get<1>(r))) == a + 3); // copy from Sent 47 assert(base(std::get<0>(std::move(r))) == a); // copy from It 48 assert(base(base(std::get<1>(std::move(r)))) == a + 3); // copy from Sent 49 } 50 { 51 using It = int*; 52 using Sent = sentinel_wrapper<int*>; 53 int a[] = {1, 2, 3}; 54 using R = std::ranges::subrange<It, Sent, std::ranges::subrange_kind::sized>; 55 R r = R(It(a), Sent(It(a + 3)), 3); 56 ASSERT_SAME_TYPE(decltype(std::get<0>(r)), It); 57 ASSERT_SAME_TYPE(decltype(std::get<1>(r)), Sent); 58 ASSERT_SAME_TYPE(decltype(std::get<0>(static_cast<R&&>(r))), It); 59 ASSERT_SAME_TYPE(decltype(std::get<1>(static_cast<R&&>(r))), Sent); 60 ASSERT_SAME_TYPE(decltype(std::get<0>(static_cast<const R&>(r))), It); 61 ASSERT_SAME_TYPE(decltype(std::get<1>(static_cast<const R&>(r))), Sent); 62 ASSERT_SAME_TYPE(decltype(std::get<0>(static_cast<const R&&>(r))), It); 63 ASSERT_SAME_TYPE(decltype(std::get<1>(static_cast<const R&&>(r))), Sent); 64 assert(base(std::get<0>(r)) == a); // copy from It 65 assert(base(base(std::get<1>(r))) == a + 3); // copy from Sent 66 assert(base(std::get<0>(std::move(r))) == a); // copy from It 67 assert(base(base(std::get<1>(std::move(r)))) == a + 3); // copy from Sent 68 } 69 { 70 // Test the fix for LWG 3589. 71 using It = cpp20_input_iterator<int*>; 72 using Sent = sentinel_wrapper<It>; 73 int a[] = {1, 2, 3}; 74 using R = std::ranges::subrange<It, Sent>; 75 R r = R(It(a), Sent(It(a + 3))); 76 static_assert(!HasGet<0, R&>); 77 ASSERT_SAME_TYPE(decltype(std::get<1>(r)), Sent); 78 ASSERT_SAME_TYPE(decltype(std::get<0>(static_cast<R&&>(r))), It); 79 ASSERT_SAME_TYPE(decltype(std::get<1>(static_cast<R&&>(r))), Sent); 80 static_assert(!HasGet<0, const R&>); 81 ASSERT_SAME_TYPE(decltype(std::get<1>(static_cast<const R&>(r))), Sent); 82 static_assert(!HasGet<0, const R&&>); 83 ASSERT_SAME_TYPE(decltype(std::get<1>(static_cast<const R&&>(r))), Sent); 84 assert(base(base(std::get<1>(r))) == a + 3); // copy from Sent 85 assert(base(std::get<0>(std::move(r))) == a); // move from It 86 assert(base(base(std::get<1>(std::move(r)))) == a + 3); // copy from Sent 87 } 88 89 return true; 90 } 91 92 int main(int, char**) { 93 test(); 94 static_assert(test()); 95 96 return 0; 97 } 98