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 | // constexpr const I& base() const &; |
12 | // constexpr I base() &&; |
13 | |
14 | #include <iterator> |
15 | |
16 | #include "test_macros.h" |
17 | #include "test_iterators.h" |
18 | |
19 | struct InputOrOutputArchetype { |
20 | using difference_type = int; |
21 | |
22 | int *ptr; |
23 | |
24 | int operator*() { return *ptr; } |
25 | void operator++(int) { ++ptr; } |
26 | InputOrOutputArchetype& operator++() { ++ptr; return *this; } |
27 | }; |
28 | |
29 | constexpr bool test() { |
30 | int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; |
31 | |
32 | { |
33 | std::counted_iterator iter(cpp20_input_iterator<int*>{buffer}, 8); |
34 | assert(base(iter.base()) == buffer); |
35 | assert(base(std::move(iter).base()) == buffer); |
36 | |
37 | ASSERT_NOEXCEPT(iter.base()); |
38 | ASSERT_SAME_TYPE(decltype(iter.base()), const cpp20_input_iterator<int*>&); |
39 | ASSERT_SAME_TYPE(decltype(std::move(iter).base()), cpp20_input_iterator<int*>); |
40 | } |
41 | |
42 | { |
43 | std::counted_iterator iter(forward_iterator<int*>{buffer}, 8); |
44 | assert(base(iter.base()) == buffer); |
45 | assert(base(std::move(iter).base()) == buffer); |
46 | |
47 | ASSERT_SAME_TYPE(decltype(iter.base()), const forward_iterator<int*>&); |
48 | ASSERT_SAME_TYPE(decltype(std::move(iter).base()), forward_iterator<int*>); |
49 | } |
50 | |
51 | { |
52 | std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8); |
53 | assert(base(iter.base()) == buffer); |
54 | assert(base(std::move(iter).base()) == buffer); |
55 | |
56 | ASSERT_SAME_TYPE(decltype(iter.base()), const contiguous_iterator<int*>&); |
57 | ASSERT_SAME_TYPE(decltype(std::move(iter).base()), contiguous_iterator<int*>); |
58 | } |
59 | |
60 | { |
61 | std::counted_iterator iter(InputOrOutputArchetype{.ptr: buffer}, 6); |
62 | assert(iter.base().ptr == buffer); |
63 | assert(std::move(iter).base().ptr == buffer); |
64 | |
65 | ASSERT_SAME_TYPE(decltype(iter.base()), const InputOrOutputArchetype&); |
66 | ASSERT_SAME_TYPE(decltype(std::move(iter).base()), InputOrOutputArchetype); |
67 | } |
68 | |
69 | { |
70 | const std::counted_iterator iter(cpp20_input_iterator<int*>{buffer}, 8); |
71 | assert(base(iter.base()) == buffer); |
72 | assert(base(std::move(iter).base()) == buffer); |
73 | |
74 | ASSERT_SAME_TYPE(decltype(iter.base()), const cpp20_input_iterator<int*>&); |
75 | ASSERT_SAME_TYPE(decltype(std::move(iter).base()), const cpp20_input_iterator<int*>&); |
76 | } |
77 | |
78 | { |
79 | const std::counted_iterator iter(forward_iterator<int*>{buffer}, 7); |
80 | assert(base(iter.base()) == buffer); |
81 | assert(base(std::move(iter).base()) == buffer); |
82 | |
83 | ASSERT_SAME_TYPE(decltype(iter.base()), const forward_iterator<int*>&); |
84 | ASSERT_SAME_TYPE(decltype(std::move(iter).base()), const forward_iterator<int*>&); |
85 | } |
86 | |
87 | { |
88 | const std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 6); |
89 | assert(base(iter.base()) == buffer); |
90 | assert(base(std::move(iter).base()) == buffer); |
91 | |
92 | ASSERT_SAME_TYPE(decltype(iter.base()), const contiguous_iterator<int*>&); |
93 | ASSERT_SAME_TYPE(decltype(std::move(iter).base()), const contiguous_iterator<int*>&); |
94 | } |
95 | |
96 | { |
97 | const std::counted_iterator iter(InputOrOutputArchetype{buffer}, 6); |
98 | assert(iter.base().ptr == buffer); |
99 | assert(std::move(iter).base().ptr == buffer); |
100 | |
101 | ASSERT_SAME_TYPE(decltype(iter.base()), const InputOrOutputArchetype&); |
102 | ASSERT_SAME_TYPE(decltype(std::move(iter).base()), const InputOrOutputArchetype&); |
103 | } |
104 | |
105 | return true; |
106 | } |
107 | |
108 | int main(int, char**) { |
109 | test(); |
110 | static_assert(test()); |
111 | |
112 | return 0; |
113 | } |
114 | |