File: arrow.pass.cpp

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (167 lines) | stat: -rw-r--r-- 5,838 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-has-no-incomplete-ranges

// constexpr InnerIter operator->() const
//   requires has-arrow<InnerIter> && copyable<InnerIter>;

#include <cassert>
#include <ranges>

#include "../types.h"

template <class T>
concept HasArrow = std::input_iterator<T> && (std::is_pointer_v<T> || requires(T i) { i.operator->(); });

template <class Base>
struct move_only_input_iter_with_arrow {
  Base it_;

  using value_type = std::iter_value_t<Base>;
  using difference_type = std::intptr_t;
  using iterator_concept = std::input_iterator_tag;

  constexpr move_only_input_iter_with_arrow(Base it) : it_(std::move(it)) {}
  constexpr move_only_input_iter_with_arrow(move_only_input_iter_with_arrow&&) = default;
  constexpr move_only_input_iter_with_arrow(const move_only_input_iter_with_arrow&) = delete;
  constexpr move_only_input_iter_with_arrow& operator=(move_only_input_iter_with_arrow&&) = default;
  constexpr move_only_input_iter_with_arrow& operator=(const move_only_input_iter_with_arrow&) = delete;

  constexpr move_only_input_iter_with_arrow& operator++() {
    ++it_;
    return *this;
  }
  constexpr void operator++(int) { ++it_; }

  constexpr std::iter_reference_t<Base> operator*() const { return *it_; }
  constexpr auto operator->() const
    requires(HasArrow<Base> && std::copyable<Base>) {
    return it_;
  }
};
static_assert(!std::copyable<move_only_input_iter_with_arrow<int*>>);
static_assert(std::input_iterator<move_only_input_iter_with_arrow<int*>>);

template <class Base>
struct move_iter_sentinel {
  Base it_;
  explicit move_iter_sentinel() = default;
  constexpr move_iter_sentinel(Base it) : it_(std::move(it)) {}
  constexpr bool operator==(const move_only_input_iter_with_arrow<Base>& other) const { return it_ == other.it_; }
};
static_assert(std::sentinel_for<move_iter_sentinel<int*>, move_only_input_iter_with_arrow<int*>>);

struct MoveOnlyIterInner : BufferView<move_only_input_iter_with_arrow<Box*>, move_iter_sentinel<Box*>> {
  using BufferView::BufferView;

  using iterator = move_only_input_iter_with_arrow<Box*>;
  using sentinel = move_iter_sentinel<Box*>;

  iterator begin() const { return data_; }
  sentinel end() const { return sentinel{data_ + size_}; }
};
static_assert(std::ranges::input_range<MoveOnlyIterInner>);

template <class Base>
struct arrow_input_iter {
  Base it_;

  using value_type = std::iter_value_t<Base>;
  using difference_type = std::intptr_t;
  using iterator_concept = std::input_iterator_tag;

  arrow_input_iter() = default;
  constexpr arrow_input_iter(Base it) : it_(std::move(it)) {}

  constexpr arrow_input_iter& operator++() {
    ++it_;
    return *this;
  }
  constexpr void operator++(int) { ++it_; }

  constexpr std::iter_reference_t<Base> operator*() const { return *it_; }
  constexpr auto operator->() const { return it_; }

  friend constexpr bool operator==(const arrow_input_iter& x, const arrow_input_iter& y) = default;
};

using ArrowInner = BufferView<arrow_input_iter<Box*>>;
static_assert(std::ranges::input_range<ArrowInner>);
static_assert(HasArrow<std::ranges::iterator_t<ArrowInner>>);

constexpr bool test() {
  Box buffer[4][4] = {{{1111}, {2222}, {3333}, {4444}},
                      {{555}, {666}, {777}, {888}},
                      {{99}, {1010}, {1111}, {1212}},
                      {{13}, {14}, {15}, {16}}};

  {
    // Copyable input iterator with arrow.
    ValueView<Box> children[4] = {ValueView(buffer[0]), ValueView(buffer[1]), ValueView(buffer[2]),
                                  ValueView(buffer[3])};
    std::ranges::join_view jv(ValueView<ValueView<Box>>{children});
    assert(jv.begin()->x == 1111);
    static_assert(HasArrow<decltype(jv.begin())>);
  }

  {
    std::ranges::join_view jv(buffer);
    assert(jv.begin()->x == 1111);
    static_assert(HasArrow<decltype(jv.begin())>);
  }

  {
    const std::ranges::join_view jv(buffer);
    assert(jv.begin()->x == 1111);
    static_assert(HasArrow<decltype(jv.begin())>);
  }

  {
    // LWG3500 `join_view::iterator::operator->()` is bogus
    // `operator->` should not be defined if inner iterator is not copyable
    // has-arrow<InnerIter> && !copyable<InnerIter>
    static_assert(HasArrow<move_only_input_iter_with_arrow<int*>>);
    MoveOnlyIterInner inners[2] = {buffer[0], buffer[1]};
    std::ranges::join_view jv{inners};
    static_assert(HasArrow<decltype(std::ranges::begin(inners[0]))>);
    static_assert(!HasArrow<decltype(jv.begin())>);
  }

  {
    // LWG3500 `join_view::iterator::operator->()` is bogus
    // `operator->` should not be defined if inner iterator does not have `operator->`
    // !has-arrow<InnerIter> && copyable<InnerIter>
    using Inner = BufferView<forward_iterator<Box*>>;
    Inner inners[2] = {buffer[0], buffer[1]};
    std::ranges::join_view jv{inners};
    static_assert(!HasArrow<decltype(std::ranges::begin(inners[0]))>);
    static_assert(!HasArrow<decltype(jv.begin())>);
  }

  {
    // arrow returns inner iterator
    ArrowInner inners[2] = {buffer[0], buffer[1]};
    std::ranges::join_view jv{inners};
    static_assert(HasArrow<decltype(std::ranges::begin(inners[0]))>);
    static_assert(HasArrow<decltype(jv.begin())>);

    auto jv_it = jv.begin();
    std::same_as<arrow_input_iter<Box*>> auto arrow_it = jv_it.operator->();
    assert(arrow_it->x == 1111);
  }
  return true;
}

int main(int, char**) {
  test();
  static_assert(test());

  return 0;
}