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// friend constexpr iterator operator-(iterator i, difference_type n);
12// friend constexpr difference_type operator-(const iterator& x, const iterator& y);
13
14#include <ranges>
15#include <cassert>
16#include <cstdint>
17#include <concepts>
18
19constexpr bool test() {
20 // <iterator> - difference_type
21 {
22 using Iter = std::ranges::iterator_t<std::ranges::repeat_view<int>>;
23 std::ranges::repeat_view<int> v(0);
24 Iter iter = v.begin() + 10;
25 assert(iter - 5 == v.begin() + 5);
26 static_assert(std::same_as<decltype(iter - 5), Iter>);
27 }
28
29 // <iterator> - <iterator>
30 {
31 // unbound
32 {
33 std::ranges::repeat_view<int> v(0);
34 auto iter1 = v.begin() + 10;
35 auto iter2 = v.begin() + 5;
36 assert(iter1 - iter2 == 5);
37 static_assert(std::same_as<decltype(iter1 - iter2), ptrdiff_t>);
38 }
39
40 // bound && signed bound sentinel
41 {
42 std::ranges::repeat_view<int, int> v(0, 20);
43 auto iter1 = v.begin() + 10;
44 auto iter2 = v.begin() + 5;
45 assert(iter1 - iter2 == 5);
46 static_assert(std::same_as<decltype(iter1 - iter2), int>);
47 }
48
49 // bound && unsigned bound sentinel
50 {
51 std::ranges::repeat_view<int, unsigned> v(0, 20);
52 auto iter1 = v.begin() + 10;
53 auto iter2 = v.begin() + 5;
54 assert(iter1 - iter2 == 5);
55 static_assert(sizeof(decltype(iter1 - iter2)) > sizeof(unsigned));
56 }
57 }
58
59 return true;
60}
61
62int main(int, char**) {
63 test();
64 static_assert(test());
65
66 return 0;
67}
68

source code of libcxx/test/std/ranges/range.factories/range.repeat.view/iterator/minus.pass.cpp