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 | |
11 | // constexpr auto size() requires sized_range<V> |
12 | // constexpr auto size() const requires sized_range<const V> |
13 | |
14 | #include <cassert> |
15 | #include <ranges> |
16 | #include <tuple> |
17 | #include <utility> |
18 | |
19 | #include "types.h" |
20 | |
21 | template <class T> |
22 | concept HasSize = requires(T t) { t.size(); }; |
23 | |
24 | static_assert(HasSize<std::ranges::elements_view<SimpleCommon, 0>>); |
25 | static_assert(HasSize<const std::ranges::elements_view<SimpleCommon, 0>>); |
26 | |
27 | struct NonSized : std::ranges::view_base { |
28 | using iterator = forward_iterator<std::tuple<int>*>; |
29 | iterator begin() const; |
30 | iterator end() const; |
31 | }; |
32 | static_assert(!std::ranges::sized_range<NonSized>); |
33 | static_assert(!std::ranges::sized_range<const NonSized>); |
34 | |
35 | static_assert(!HasSize<std::ranges::elements_view<NonSized, 0>>); |
36 | static_assert(!HasSize<const std::ranges::elements_view<NonSized, 0>>); |
37 | |
38 | struct SizedNonConst : TupleBufferView { |
39 | using TupleBufferView::TupleBufferView; |
40 | |
41 | using iterator = forward_iterator<std::tuple<int>*>; |
42 | constexpr auto begin() const { return iterator{buffer_}; } |
43 | constexpr auto end() const { return iterator{buffer_ + size_}; } |
44 | constexpr std::size_t size() { return size_; } |
45 | }; |
46 | |
47 | static_assert(HasSize<std::ranges::elements_view<SizedNonConst, 0>>); |
48 | static_assert(!HasSize<const std::ranges::elements_view<SizedNonConst, 0>>); |
49 | |
50 | struct OnlyConstSized : TupleBufferView { |
51 | using TupleBufferView::TupleBufferView; |
52 | |
53 | using iterator = forward_iterator<std::tuple<int>*>; |
54 | constexpr auto begin() const { return iterator{buffer_}; } |
55 | constexpr auto end() const { return iterator{buffer_ + size_}; } |
56 | constexpr std::size_t size() const { return size_; } |
57 | constexpr std::size_t size() = delete; |
58 | }; |
59 | |
60 | static_assert(HasSize<const OnlyConstSized>); |
61 | static_assert(HasSize<std::ranges::elements_view<OnlyConstSized, 0>>); |
62 | static_assert(HasSize<const std::ranges::elements_view<OnlyConstSized, 0>>); |
63 | |
64 | constexpr bool test() { |
65 | std::tuple<int> buffer[] = {{1}, {2}, {3}}; |
66 | |
67 | // non-const and const are sized |
68 | { |
69 | auto ev = std::views::elements<0>(buffer); |
70 | assert(ev.size() == 3); |
71 | assert(std::as_const(ev).size() == 3); |
72 | } |
73 | |
74 | { |
75 | // const-view non-sized range |
76 | auto ev = std::views::elements<0>(SizedNonConst{buffer}); |
77 | assert(ev.size() == 3); |
78 | } |
79 | |
80 | return true; |
81 | } |
82 | |
83 | int main(int, char**) { |
84 | test(); |
85 | static_assert(test()); |
86 | |
87 | return 0; |
88 | } |
89 | |