xref: /llvm-project/libcxx/test/std/iterators/predef.iterators/iterators.common/arrow.pass.cpp (revision f03430f5e37e8eb64878dc538b05210adea2d80f)
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 // auto operator->() const
12 //   requires see below;
13 
14 #include <iterator>
15 #include <cassert>
16 #include <concepts>
17 
18 #include "test_iterators.h"
19 #include "test_macros.h"
20 #include "types.h"
21 
test()22 void test() {
23   // Case 1: http://eel.is/c++draft/iterators.common#common.iter.access-5.1
24   {
25     auto check = []<class Iterator>() {
26       int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
27       Iterator iter(buffer);
28       using Common = std::common_iterator<Iterator, sentinel_wrapper<Iterator>>;
29 
30       Common common(iter);
31       std::same_as<Iterator> decltype(auto) result = common.operator->();
32       assert(base(result) == buffer);
33 
34       Common const ccommon(iter);
35       std::same_as<Iterator> decltype(auto) cresult = ccommon.operator->();
36       assert(base(cresult) == buffer);
37     };
38 
39     check.operator()<contiguous_iterator<int*>>();
40     check.operator()<int*>();
41   }
42 
43   // Case 2: http://eel.is/c++draft/iterators.common#common.iter.access-5.2
44   {
45     auto check = []<class Iterator>() {
46       int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
47       Iterator iter(buffer);
48       using Common = std::common_iterator<Iterator, sentinel_type<int*>>;
49 
50       Common common(iter);
51       std::same_as<int*> decltype(auto) result = common.operator->();
52       assert(result == buffer);
53 
54       Common const ccommon(iter);
55       std::same_as<int*> decltype(auto) cresult = ccommon.operator->();
56       assert(cresult == buffer);
57     };
58 
59     check.operator()<simple_iterator<int*>>();
60     check.operator()<cpp17_input_iterator<int*>>();
61     // cpp20_input_iterator can't be used with common_iterator because it's not copyable
62     check.operator()<forward_iterator<int*>>();
63     check.operator()<bidirectional_iterator<int*>>();
64     check.operator()<random_access_iterator<int*>>();
65   }
66 
67   // Case 3: http://eel.is/c++draft/iterators.common#common.iter.access-5.3
68   {
69     auto check = []<class Iterator>() {
70       int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
71       Iterator iter(buffer);
72       using Common = std::common_iterator<Iterator, sentinel_type<int*>>;
73 
74       Common common(iter);
75       auto proxy                                     = common.operator->();
76       std::same_as<int const*> decltype(auto) result = proxy.operator->();
77       assert(result != buffer); // we copied to a temporary proxy
78       assert(*result == *buffer);
79 
80       Common const ccommon(iter);
81       auto cproxy                                     = ccommon.operator->();
82       std::same_as<int const*> decltype(auto) cresult = cproxy.operator->();
83       assert(cresult != buffer); // we copied to a temporary proxy
84       assert(*cresult == *buffer);
85     };
86 
87     check.operator()<value_iterator<int*>>();
88     check.operator()<void_plus_plus_iterator<int*>>();
89   }
90 }
91 
main(int,char **)92 int main(int, char**) {
93   test();
94 
95   return 0;
96 }
97