1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
13 // template <RandomAccessIterator Iter1, RandomAccessIterator Iter2>
14 // requires HasMinus<Iter2, Iter1>
15 // auto operator-(const reverse_iterator<Iter1>& x, const reverse_iterator<Iter2>& y) // constexpr in C++17
16 // -> decltype(y.base() - x.base());
21 #include <type_traits>
23 #include "test_macros.h"
24 #include "test_iterators.h"
26 template <class, class, class = void>
27 struct HasMinus
: std::false_type
{};
28 template <class R1
, class R2
>
29 struct HasMinus
<R1
, R2
, decltype((R1() - R2(), void()))> : std::true_type
{};
31 // Test non-subtractable base iterator types
32 static_assert(HasMinus
<std::reverse_iterator
<int*>, std::reverse_iterator
<int*> >::value
, "");
33 static_assert(HasMinus
<std::reverse_iterator
<int*>, std::reverse_iterator
<const int*> >::value
, "");
35 #if TEST_STD_VER >= 11
36 static_assert(!HasMinus
<std::reverse_iterator
<int*>, std::reverse_iterator
<char*> >::value
, "");
37 static_assert(!HasMinus
<std::reverse_iterator
<bidirectional_iterator
<int*> >,
38 std::reverse_iterator
<bidirectional_iterator
<int*> > >::value
,
42 template <class It1
, class It2
>
43 TEST_CONSTEXPR_CXX17
void test_one(It1 l
, It2 r
, std::ptrdiff_t x
) {
44 const std::reverse_iterator
<It1
> r1(l
);
45 const std::reverse_iterator
<It2
> r2(r
);
46 assert((r1
- r2
) == x
);
50 TEST_CONSTEXPR_CXX17
void test() {
51 // Test same base iterator type
54 test_one(Iter(s
), Iter(s
), 0);
55 test_one(Iter(s
), Iter(s
+ 1), 1);
56 test_one(Iter(s
+ 1), Iter(s
), -1);
59 TEST_CONSTEXPR_CXX17
bool tests() {
62 test
<random_access_iterator
<char*> >();
63 #if TEST_STD_VER >= 20
64 test
<cpp20_random_access_iterator
<char*>>();
68 // Test different (but subtractable) base iterator types
69 using PC
= const char*;
71 test_one(PC(s
), s
, 0);
72 test_one(PC(s
), s
+ 1, 1);
73 test_one(PC(s
+ 1), s
, -1);
79 int main(int, char**) {
82 static_assert(tests(), "");