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 | // move_iterator |
12 | |
13 | // template <InputIterator Iter1, InputIterator Iter2> |
14 | // requires HasEqualTo<Iter1, Iter2> |
15 | // bool |
16 | // operator!=(const move_iterator<Iter1>& x, const move_iterator<Iter2>& y); |
17 | // |
18 | // constexpr in C++17 |
19 | |
20 | #include <iterator> |
21 | #include <cassert> |
22 | |
23 | #include "test_macros.h" |
24 | #include "test_iterators.h" |
25 | |
26 | // In C++17, move_iterator's operator!= calls the underlying iterator's operator!= |
27 | // In C++20, move_iterator's operator== calls the underlying iterator's operator== |
28 | struct CustomIt { |
29 | using value_type = int; |
30 | using difference_type = int; |
31 | using reference = int&; |
32 | using pointer = int*; |
33 | using iterator_category = std::input_iterator_tag; |
34 | CustomIt() = default; |
35 | TEST_CONSTEXPR_CXX17 explicit CustomIt(int* p) : p_(p) {} |
36 | int& operator*() const; |
37 | CustomIt& operator++(); |
38 | CustomIt operator++(int); |
39 | #if TEST_STD_VER > 17 |
40 | friend constexpr bool operator==(const CustomIt& a, const CustomIt& b) { return a.p_ == b.p_; } |
41 | friend bool operator!=(const CustomIt& a, const CustomIt& b) = delete; |
42 | #else |
43 | friend TEST_CONSTEXPR_CXX17 bool operator!=(const CustomIt& a, const CustomIt& b) { return a.p_ != b.p_; } |
44 | #endif |
45 | int *p_ = nullptr; |
46 | }; |
47 | |
48 | template <class It> |
49 | TEST_CONSTEXPR_CXX17 void test_one() |
50 | { |
51 | int a[] = {3, 1, 4}; |
52 | const std::move_iterator<It> r1 = std::move_iterator<It>(It(a)); |
53 | const std::move_iterator<It> r2 = std::move_iterator<It>(It(a)); |
54 | const std::move_iterator<It> r3 = std::move_iterator<It>(It(a + 2)); |
55 | ASSERT_SAME_TYPE(decltype(r1 != r2), bool); |
56 | assert(!(r1 != r1)); |
57 | assert(!(r1 != r2)); |
58 | assert(!(r2 != r1)); |
59 | assert( (r1 != r3)); |
60 | assert( (r3 != r1)); |
61 | } |
62 | |
63 | TEST_CONSTEXPR_CXX17 bool test() |
64 | { |
65 | test_one<CustomIt>(); |
66 | test_one<cpp17_input_iterator<int*> >(); |
67 | test_one<forward_iterator<int*> >(); |
68 | test_one<bidirectional_iterator<int*> >(); |
69 | test_one<random_access_iterator<int*> >(); |
70 | test_one<int*>(); |
71 | test_one<const int*>(); |
72 | |
73 | #if TEST_STD_VER > 17 |
74 | test_one<contiguous_iterator<int*>>(); |
75 | test_one<three_way_contiguous_iterator<int*>>(); |
76 | #endif |
77 | |
78 | return true; |
79 | } |
80 | |
81 | int main(int, char**) |
82 | { |
83 | test(); |
84 | #if TEST_STD_VER > 14 |
85 | static_assert(test()); |
86 | #endif |
87 | |
88 | return 0; |
89 | } |
90 | |