| 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 | // template<class I, sentinel_for<I> S> |
| 12 | // requires (!sized_sentinel_for<S, I>) |
| 13 | // constexpr iter_difference_t<I> ranges::distance(I first, S last); |
| 14 | // |
| 15 | // template<class I, sized_sentinel_for<decay_t<I>> S> |
| 16 | // constexpr iter_difference_t<I> ranges::distance(const I& first, S last); |
| 17 | |
| 18 | #include <iterator> |
| 19 | #include <cassert> |
| 20 | |
| 21 | #include "test_iterators.h" |
| 22 | |
| 23 | template<class It> |
| 24 | struct EvilSentinel { |
| 25 | It p_; |
| 26 | friend constexpr bool operator==(EvilSentinel s, It p) { return s.p_ == p; } |
| 27 | friend constexpr auto operator-(EvilSentinel s, It p) { return s.p_ - p; } |
| 28 | friend constexpr auto operator-(It p, EvilSentinel s) { return p - s.p_; } |
| 29 | friend constexpr void operator-(EvilSentinel s, int(&)[3]) = delete; |
| 30 | friend constexpr void operator-(EvilSentinel s, int(&&)[3]) = delete; |
| 31 | friend constexpr void operator-(EvilSentinel s, const int(&)[3]) = delete; |
| 32 | friend constexpr void operator-(EvilSentinel s, const int(&&)[3]) = delete; |
| 33 | }; |
| 34 | static_assert( std::sized_sentinel_for<EvilSentinel<int*>, int*>); |
| 35 | static_assert(!std::sized_sentinel_for<EvilSentinel<int*>, const int*>); |
| 36 | static_assert( std::sized_sentinel_for<EvilSentinel<const int*>, int*>); |
| 37 | static_assert( std::sized_sentinel_for<EvilSentinel<const int*>, const int*>); |
| 38 | |
| 39 | constexpr bool test() { |
| 40 | { |
| 41 | int a[] = {1, 2, 3}; |
| 42 | assert(std::ranges::distance(a, a + 3) == 3); |
| 43 | assert(std::ranges::distance(a, a) == 0); |
| 44 | assert(std::ranges::distance(a + 3, a) == -3); |
| 45 | } |
| 46 | { |
| 47 | int a[] = {1, 2, 3}; |
| 48 | assert(std::ranges::distance(a, EvilSentinel<int*>{a+3}) == 3); |
| 49 | assert(std::ranges::distance(a, EvilSentinel<int*>{a}) == 0); |
| 50 | assert(std::ranges::distance(a+3, EvilSentinel<int*>{a}) == -3); |
| 51 | assert(std::ranges::distance(std::move(a), EvilSentinel<int*>{a+3}) == 3); |
| 52 | } |
| 53 | { |
| 54 | const int a[] = {1, 2, 3}; |
| 55 | assert(std::ranges::distance(a, EvilSentinel<const int*>{a+3}) == 3); |
| 56 | assert(std::ranges::distance(a, EvilSentinel<const int*>{a}) == 0); |
| 57 | assert(std::ranges::distance(a+3, EvilSentinel<const int*>{a}) == -3); |
| 58 | assert(std::ranges::distance(std::move(a), EvilSentinel<const int*>{a+3}) == 3); |
| 59 | static_assert(!std::is_invocable_v<decltype(std::ranges::distance), const int(&)[3], EvilSentinel<int*>>); |
| 60 | static_assert(!std::is_invocable_v<decltype(std::ranges::distance), const int(&&)[3], EvilSentinel<int*>>); |
| 61 | } |
| 62 | |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | int main(int, char**) { |
| 67 | test(); |
| 68 | static_assert(test()); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |