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, c++20
10 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
11 
12 // constexpr sentinel(sentinel<!Const> s);
13 
14 #include <cassert>
15 #include <ranges>
16 
17 #include "../types.h"
18 
19 template <class T>
20 struct convertible_sentinel_wrapper {
21   explicit convertible_sentinel_wrapper() = default;
22   constexpr convertible_sentinel_wrapper(const T& it) : it_(it) {}
23 
24   template <class U>
25     requires std::convertible_to<const U&, T>
26   constexpr convertible_sentinel_wrapper(const convertible_sentinel_wrapper<U>& other) : it_(other.it_) {}
27 
28   constexpr friend bool operator==(convertible_sentinel_wrapper const& self, const T& other) {
29     return self.it_ == other;
30   }
31   T it_;
32 };
33 
34 struct NonSimpleNonCommonConvertibleView : IntBufferView {
35   using IntBufferView::IntBufferView;
36 
37   constexpr int* begin() { return buffer_; }
38   constexpr const int* begin() const { return buffer_; }
39   constexpr convertible_sentinel_wrapper<int*> end() { return convertible_sentinel_wrapper<int*>(buffer_ + size_); }
40   constexpr convertible_sentinel_wrapper<const int*> end() const {
41     return convertible_sentinel_wrapper<const int*>(buffer_ + size_);
42   }
43 };
44 
45 static_assert(!std::ranges::common_range<NonSimpleNonCommonConvertibleView>);
46 static_assert(std::ranges::random_access_range<NonSimpleNonCommonConvertibleView>);
47 static_assert(!std::ranges::sized_range<NonSimpleNonCommonConvertibleView>);
48 static_assert(std::convertible_to<std::ranges::sentinel_t<NonSimpleNonCommonConvertibleView>,
49                                   std::ranges::sentinel_t<NonSimpleNonCommonConvertibleView const>>);
50 LIBCPP_STATIC_ASSERT(!std::ranges::__simple_view<NonSimpleNonCommonConvertibleView>);
51 
52 constexpr bool test() {
53   int buffer1[4] = {1, 2, 3, 4};
54   int buffer2[5] = {1, 2, 3, 4, 5};
55   std::ranges::zip_view v{NonSimpleNonCommonConvertibleView(buffer1), NonSimpleNonCommonConvertibleView(buffer2)};
56   static_assert(!std::ranges::common_range<decltype(v)>);
57   auto sent1 = v.end();
58   std::ranges::sentinel_t<const decltype(v)> sent2 = sent1;
59   static_assert(!std::is_same_v<decltype(sent1), decltype(sent2)>);
60 
61   assert(v.begin() != sent2);
62   assert(std::as_const(v).begin() != sent2);
63   assert(v.begin() + 4 == sent2);
64   assert(std::as_const(v).begin() + 4 == sent2);
65 
66   // Cannot create a non-const iterator from a const iterator.
67   static_assert(!std::constructible_from<decltype(sent1), decltype(sent2)>);
68   return true;
69 }
70 
71 int main(int, char**) {
72   test();
73   static_assert(test());
74 
75   return 0;
76 }
77