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 // UNSUPPORTED: libcpp-no-concepts 11 // UNSUPPORTED: gcc-10 12 13 // template<common_with<I> I2> 14 // friend constexpr iter_difference_t<I2> operator-( 15 // const counted_iterator& x, const counted_iterator<I2>& y); 16 17 #include <iterator> 18 19 #include "test_macros.h" 20 #include "test_iterators.h" 21 22 // This iterator is common_with forward_iterator but NOT comparable with it. 23 template <class It> 24 class CommonWithForwardIter 25 { 26 It it_; 27 28 public: 29 typedef std::input_iterator_tag iterator_category; 30 typedef typename std::iterator_traits<It>::value_type value_type; 31 typedef typename std::iterator_traits<It>::difference_type difference_type; 32 typedef It pointer; 33 typedef typename std::iterator_traits<It>::reference reference; 34 35 constexpr It base() const {return it_;} 36 37 CommonWithForwardIter() = default; 38 explicit constexpr CommonWithForwardIter(It it) : it_(it) {} 39 constexpr CommonWithForwardIter(const forward_iterator<It>& it) : it_(it.base()) {} 40 41 constexpr reference operator*() const {return *it_;} 42 43 constexpr CommonWithForwardIter& operator++() {++it_; return *this;} 44 constexpr CommonWithForwardIter operator++(int) 45 {CommonWithForwardIter tmp(*this); ++(*this); return tmp;} 46 }; 47 48 constexpr bool test() { 49 int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; 50 51 { 52 std::counted_iterator iter1(random_access_iterator<int*>{buffer}, 8); 53 std::counted_iterator iter2(random_access_iterator<int*>{buffer + 4}, 4); 54 assert(iter1 - iter2 == -4); 55 assert(iter2 - iter1 == 4); 56 assert(iter1.count() == 8); 57 assert(iter2.count() == 4); 58 59 ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>); 60 } 61 { 62 const std::counted_iterator iter1(random_access_iterator<int*>{buffer}, 8); 63 const std::counted_iterator iter2(random_access_iterator<int*>{buffer + 4}, 4); 64 assert(iter1 - iter2 == -4); 65 assert(iter2 - iter1 == 4); 66 assert(iter1.count() == 8); 67 assert(iter2.count() == 4); 68 69 ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>); 70 } 71 { 72 std::counted_iterator iter1(contiguous_iterator<int*>{buffer}, 8); 73 std::counted_iterator iter2(contiguous_iterator<int*>{buffer + 2}, 6); 74 assert(iter1 - iter2 == -2); 75 assert(iter2 - iter1 == 2); 76 assert(iter1.count() == 8); 77 assert(iter2.count() == 6); 78 79 ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>); 80 } 81 { 82 const std::counted_iterator iter1(contiguous_iterator<int*>{buffer}, 8); 83 const std::counted_iterator iter2(contiguous_iterator<int*>{buffer + 2}, 6); 84 assert(iter1 - iter2 == -2); 85 assert(iter2 - iter1 == 2); 86 assert(iter1.count() == 8); 87 assert(iter2.count() == 6); 88 89 ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>); 90 } 91 // The minus operator works even if Iter is not a random_access_iterator because 92 // counted_iterator is able to implement it by subtracting the counts rather than 93 // the underlying iterators. 94 { 95 std::counted_iterator iter1(CommonWithForwardIter<int*>{buffer}, 8); 96 std::counted_iterator iter2(forward_iterator<int*>{buffer + 2}, 6); 97 assert(iter1 - iter2 == -2); 98 assert(iter2 - iter1 == 2); 99 assert(iter1.count() == 8); 100 assert(iter2.count() == 6); 101 102 ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>); 103 } 104 { 105 const std::counted_iterator iter1(CommonWithForwardIter<int*>{buffer}, 8); 106 const std::counted_iterator iter2(forward_iterator<int*>{buffer + 2}, 6); 107 assert(iter1 - iter2 == -2); 108 assert(iter2 - iter1 == 2); 109 assert(iter1.count() == 8); 110 assert(iter2.count() == 6); 111 112 ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>); 113 } 114 115 return true; 116 } 117 118 int main(int, char**) { 119 test(); 120 static_assert(test()); 121 122 return 0; 123 } 124