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