| 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 | // reference operator*() const; |
| 14 | // |
| 15 | // constexpr in C++17 |
| 16 | |
| 17 | #include <iterator> |
| 18 | #include <cassert> |
| 19 | #include <memory> |
| 20 | |
| 21 | #include "test_iterators.h" |
| 22 | #include "test_macros.h" |
| 23 | |
| 24 | class A |
| 25 | { |
| 26 | int data_; |
| 27 | public: |
| 28 | A() : data_(1) {} |
| 29 | A(const A&) = default; |
| 30 | A& operator=(const A&) = default; |
| 31 | ~A() {data_ = -1;} |
| 32 | |
| 33 | friend bool operator==(const A& x, const A& y) |
| 34 | {return x.data_ == y.data_;} |
| 35 | }; |
| 36 | |
| 37 | template <class It> |
| 38 | void |
| 39 | test(It i, typename std::iterator_traits<It>::value_type x) |
| 40 | { |
| 41 | std::move_iterator<It> r(i); |
| 42 | assert(*r == x); |
| 43 | typename std::iterator_traits<It>::value_type x2 = *r; |
| 44 | assert(x2 == x); |
| 45 | } |
| 46 | |
| 47 | struct do_nothing |
| 48 | { |
| 49 | void operator()(void*) const {} |
| 50 | }; |
| 51 | |
| 52 | |
| 53 | int main(int, char**) { |
| 54 | { |
| 55 | A a; |
| 56 | test(i: &a, x: A()); |
| 57 | } |
| 58 | #if TEST_STD_VER >= 11 |
| 59 | { |
| 60 | int i; |
| 61 | std::unique_ptr<int, do_nothing> p(&i); |
| 62 | test(&p, std::unique_ptr<int, do_nothing>(&i)); |
| 63 | } |
| 64 | #endif |
| 65 | #if TEST_STD_VER > 14 |
| 66 | { |
| 67 | constexpr const char *p = "123456789" ; |
| 68 | typedef std::move_iterator<const char *> MI; |
| 69 | constexpr MI it1 = std::make_move_iterator(p); |
| 70 | constexpr MI it2 = std::make_move_iterator(p+1); |
| 71 | static_assert(*it1 == p[0], "" ); |
| 72 | static_assert(*it2 == p[1], "" ); |
| 73 | } |
| 74 | #endif |
| 75 | |
| 76 | #if TEST_STD_VER > 17 |
| 77 | // Ensure the `iter_move` customization point is being used. |
| 78 | { |
| 79 | int a[] = {0, 1, 2}; |
| 80 | |
| 81 | int iter_moves = 0; |
| 82 | adl::Iterator i = adl::Iterator::TrackMoves(a, iter_moves); |
| 83 | std::move_iterator<adl::Iterator> mi(i); |
| 84 | |
| 85 | auto x = *mi; |
| 86 | assert(x == 0); |
| 87 | assert(iter_moves == 1); |
| 88 | } |
| 89 | #endif |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |