| 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 | // friend constexpr iter_difference_t<W> operator-(const iterator& x, const sentinel& y) |
| 12 | // requires sized_sentinel_for<Bound, W>; |
| 13 | // friend constexpr iter_difference_t<W> operator-(const sentinel& x, const iterator& y) |
| 14 | // requires sized_sentinel_for<Bound, W>; |
| 15 | |
| 16 | #include <ranges> |
| 17 | #include <cassert> |
| 18 | |
| 19 | #include "test_macros.h" |
| 20 | #include "test_iterators.h" |
| 21 | #include "../types.h" |
| 22 | |
| 23 | template<class T> |
| 24 | concept MinusInvocable = requires(std::ranges::iota_view<T, IntSentinelWith<T>> io) { |
| 25 | io.end() - io.begin(); |
| 26 | }; |
| 27 | |
| 28 | constexpr bool test() { |
| 29 | int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 30 | |
| 31 | { |
| 32 | auto outIter = random_access_iterator<int*>(buffer); |
| 33 | std::ranges::iota_view<random_access_iterator<int*>, IntSentinelWith<random_access_iterator<int*>>> io( |
| 34 | outIter, IntSentinelWith<random_access_iterator<int*>>(std::ranges::next(outIter, 8))); |
| 35 | auto iter = io.begin(); |
| 36 | auto sent = io.end(); |
| 37 | assert(iter - sent == -8); |
| 38 | assert(sent - iter == 8); |
| 39 | } |
| 40 | { |
| 41 | auto outIter = random_access_iterator<int*>(buffer); |
| 42 | const std::ranges::iota_view<random_access_iterator<int*>, IntSentinelWith<random_access_iterator<int*>>> io( |
| 43 | outIter, IntSentinelWith<random_access_iterator<int*>>(std::ranges::next(outIter, 8))); |
| 44 | const auto iter = io.begin(); |
| 45 | const auto sent = io.end(); |
| 46 | assert(iter - sent == -8); |
| 47 | assert(sent - iter == 8); |
| 48 | } |
| 49 | |
| 50 | { |
| 51 | // The minus operator requires that "W" is an input_or_output_iterator. |
| 52 | static_assert(!MinusInvocable<int>); |
| 53 | } |
| 54 | |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | int main(int, char**) { |
| 59 | test(); |
| 60 | static_assert(test()); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |