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