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// constexpr auto size()
10// constexpr auto size() const
11
12// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
13
14#include <cassert>
15#include <cstddef>
16#include <ranges>
17
18struct ConstSizedView : std::ranges::view_base {
19 bool* size_called;
20 int* begin() const;
21 int* end() const;
22
23 constexpr std::size_t size() const {
24 *size_called = true;
25 return 3;
26 }
27};
28
29struct SizedView : std::ranges::view_base {
30 bool* size_called;
31 int* begin() const;
32 int* end() const;
33
34 constexpr int size() {
35 *size_called = true;
36 return 5;
37 }
38};
39
40struct UnsizedView : std::ranges::view_base {
41 int* begin() const;
42 int* end() const;
43};
44
45template <class T>
46concept HasSize = requires(T v) { v.size(); };
47
48static_assert(HasSize<ConstSizedView>);
49static_assert(HasSize<const ConstSizedView>);
50static_assert(HasSize<SizedView>);
51static_assert(!HasSize<const SizedView>);
52static_assert(!HasSize<UnsizedView>);
53static_assert(!HasSize<const UnsizedView>);
54
55constexpr bool test() {
56 {
57 bool size_called = false;
58 std::ranges::as_rvalue_view view(ConstSizedView{{}, &size_called});
59 std::same_as<std::size_t> auto size = view.size();
60 assert(size == 3);
61 assert(size_called);
62 }
63
64 {
65 bool size_called = false;
66 std::ranges::as_rvalue_view view(SizedView{{}, &size_called});
67 std::same_as<int> auto size = view.size();
68 assert(size == 5);
69 assert(size_called);
70 }
71
72 return true;
73}
74
75int main(int, char**) {
76 test();
77 static_assert(test());
78
79 return 0;
80}
81

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