| 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 |
// <iterator> |
| 10 |
|
| 11 |
// reverse_iterator |
| 12 |
|
| 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()); |
| 17 |
|
| 18 |
#include <iterator> |
| 19 |
#include <cstddef> |
| 20 |
#include <cassert> |
| 21 |
#include <type_traits> |
| 22 |
|
| 23 |
#include "test_macros.h" |
| 24 |
#include "test_iterators.h" |
| 25 |
|
| 26 |
template <class, class, class = void> struct HasMinus : std::false_type {}; |
| 27 |
template <class R1, class R2> struct HasMinus<R1, R2, decltype((R1() - R2(), void()))> : std::true_type {}; |
| 28 |
|
| 29 |
template <class It1, class It2> |
| 30 |
TEST_CONSTEXPR_CXX17 void test(It1 l, It2 r, std::ptrdiff_t x) { |
| 31 |
const std::reverse_iterator<It1> r1(l); |
| 32 |
const std::reverse_iterator<It2> r2(r); |
| 33 |
assert((r1 - r2) == x); |
| 34 |
} |
| 35 |
|
| 36 |
TEST_CONSTEXPR_CXX17 bool tests() { |
| 37 |
using PC = const char*; |
| 38 |
char s[3] = {0}; |
| 39 |
|
| 40 |
// Test same base iterator type |
| 41 |
test(s, s, 0); |
| 42 |
test(s, s+1, 1); |
| 43 |
test(s+1, s, -1); |
| 44 |
|
| 45 |
// Test different (but subtractable) base iterator types |
| 46 |
test(PC(s), s, 0); |
| 47 |
test(PC(s), s+1, 1); |
| 48 |
test(PC(s+1), s, -1); |
| 49 |
|
| 50 |
// Test non-subtractable base iterator types |
| 51 |
static_assert( HasMinus<std::reverse_iterator<int*>, std::reverse_iterator<int*> >::value, ""); |
| 52 |
static_assert( HasMinus<std::reverse_iterator<int*>, std::reverse_iterator<const int*> >::value, ""); |
| 53 |
#if TEST_STD_VER >= 11 |
| 54 |
static_assert(!HasMinus<std::reverse_iterator<int*>, std::reverse_iterator<char*> >::value, ""); |
| 55 |
static_assert(!HasMinus<std::reverse_iterator<bidirectional_iterator<int*> >, std::reverse_iterator<bidirectional_iterator<int*> > >::value, ""); |
| 56 |
#endif |
| 57 |
|
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
int main(int, char**) { |
| 62 |
tests(); |
| 63 |
#if TEST_STD_VER > 14 |
| 64 |
static_assert(tests(), ""); |
| 65 |
#endif |
| 66 |
return 0; |
| 67 |
} |