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
21template <class T>
22concept HasSize = requires(T t) { t.size(); };
23
24static_assert(HasSize<std::ranges::elements_view<SimpleCommon, 0>>);
25static_assert(HasSize<const std::ranges::elements_view<SimpleCommon, 0>>);
26
27struct NonSized : std::ranges::view_base {
28 using iterator = forward_iterator<std::tuple<int>*>;
29 iterator begin() const;
30 iterator end() const;
31};
32static_assert(!std::ranges::sized_range<NonSized>);
33static_assert(!std::ranges::sized_range<const NonSized>);
34
35static_assert(!HasSize<std::ranges::elements_view<NonSized, 0>>);
36static_assert(!HasSize<const std::ranges::elements_view<NonSized, 0>>);
37
38struct 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
47static_assert(HasSize<std::ranges::elements_view<SizedNonConst, 0>>);
48static_assert(!HasSize<const std::ranges::elements_view<SizedNonConst, 0>>);
49
50struct 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
60static_assert(HasSize<const OnlyConstSized>);
61static_assert(HasSize<std::ranges::elements_view<OnlyConstSized, 0>>);
62static_assert(HasSize<const std::ranges::elements_view<OnlyConstSized, 0>>);
63
64constexpr 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
83int main(int, char**) {
84 test();
85 static_assert(test());
86
87 return 0;
88}
89

source code of libcxx/test/std/ranges/range.adaptors/range.elements/size.pass.cpp