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 // requires RandomAccessIterator<Iter>
14 // unspecified operator[](difference_type n) const;
15 //
16 // constexpr in C++17
17
18 #include <iterator>
19 #include <cassert>
20 #include <memory>
21
22 #include "test_macros.h"
23 #include "test_iterators.h"
24
25 template <class It>
26 void
test(It i,typename std::iterator_traits<It>::difference_type n,typename std::iterator_traits<It>::value_type x)27 test(It i, typename std::iterator_traits<It>::difference_type n,
28 typename std::iterator_traits<It>::value_type x)
29 {
30 typedef typename std::iterator_traits<It>::value_type value_type;
31 const std::move_iterator<It> r(i);
32 value_type rr = r[n];
33 assert(rr == x);
34 }
35
36 struct do_nothing
37 {
operator ()do_nothing38 void operator()(void*) const {}
39 };
40
main(int,char **)41 int main(int, char**)
42 {
43 {
44 char s[] = "1234567890";
45 test(random_access_iterator<char*>(s+5), 4, '0');
46 test(s+5, 4, '0');
47 }
48 #if TEST_STD_VER >= 11
49 {
50 int i[5];
51 typedef std::unique_ptr<int, do_nothing> Ptr;
52 Ptr p[5];
53 for (unsigned j = 0; j < 5; ++j)
54 p[j].reset(i+j);
55 test(p, 3, Ptr(i+3));
56 }
57 #endif
58 #if TEST_STD_VER > 14
59 {
60 constexpr const char *p = "123456789";
61 typedef std::move_iterator<const char *> MI;
62 constexpr MI it1 = std::make_move_iterator(p);
63 static_assert(it1[0] == '1', "");
64 static_assert(it1[5] == '6', "");
65 }
66 #endif
67
68 return 0;
69 }
70