xref: /llvm-project/libcxx/test/std/iterators/predef.iterators/iterators.common/iter_move.pass.cpp (revision d2baefae6846765eef6a6dd69d4fdf1082ce29ad)
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 // friend iter_rvalue_reference_t<I> iter_move(const common_iterator& i)
12 //   noexcept(noexcept(ranges::iter_move(declval<const I&>())))
13 //     requires input_iterator<I>;
14 
15 #include <iterator>
16 #include <cassert>
17 #include <type_traits>
18 
19 #include "test_iterators.h"
20 #include "test_macros.h"
21 
22 struct IterMovingIt {
23   using value_type = int;
24   using difference_type = int;
25   explicit IterMovingIt() = default;
26   IterMovingIt(const IterMovingIt&); // copyable, but this test shouldn't make copies
27   IterMovingIt(IterMovingIt&&) = default;
28   IterMovingIt& operator=(const IterMovingIt&);
29   int& operator*() const;
operator ++IterMovingIt30   constexpr IterMovingIt& operator++() { return *this; }
31   IterMovingIt operator++(int);
iter_move(const IterMovingIt &)32   friend constexpr int iter_move(const IterMovingIt&) {
33     return 42;
34   }
35   bool operator==(std::default_sentinel_t) const;
36 };
37 static_assert(std::input_iterator<IterMovingIt>);
38 
test()39 constexpr bool test() {
40   {
41     using It = int*;
42     using CommonIt = std::common_iterator<It, sentinel_wrapper<It>>;
43     int a[] = {1, 2, 3};
44     CommonIt it = CommonIt(It(a));
45     ASSERT_NOEXCEPT(iter_move(it));
46     ASSERT_NOEXCEPT(std::ranges::iter_move(it));
47     ASSERT_SAME_TYPE(decltype(iter_move(it)), int&&);
48     ASSERT_SAME_TYPE(decltype(std::ranges::iter_move(it)), int&&);
49     assert(iter_move(it) == 1);
50     if (!std::is_constant_evaluated()) {
51       ++it;
52       assert(iter_move(it) == 2);
53     }
54   }
55   {
56     using It = const int*;
57     using CommonIt = std::common_iterator<It, sentinel_wrapper<It>>;
58     int a[] = {1, 2, 3};
59     CommonIt it = CommonIt(It(a));
60     ASSERT_NOEXCEPT(iter_move(it));
61     ASSERT_NOEXCEPT(std::ranges::iter_move(it));
62     ASSERT_SAME_TYPE(decltype(iter_move(it)), const int&&);
63     ASSERT_SAME_TYPE(decltype(std::ranges::iter_move(it)), const int&&);
64     assert(iter_move(it) == 1);
65     if (!std::is_constant_evaluated()) {
66       ++it;
67       assert(iter_move(it) == 2);
68     }
69   }
70   {
71     using It = IterMovingIt;
72     using CommonIt = std::common_iterator<It, std::default_sentinel_t>;
73     CommonIt it = CommonIt(It());
74     ASSERT_NOT_NOEXCEPT(iter_move(it));
75     ASSERT_NOT_NOEXCEPT(std::ranges::iter_move(it));
76     ASSERT_SAME_TYPE(decltype(iter_move(it)), int);
77     ASSERT_SAME_TYPE(decltype(std::ranges::iter_move(it)), int);
78     assert(iter_move(it) == 42);
79     if (!std::is_constant_evaluated()) {
80       ++it;
81       assert(iter_move(it) == 42);
82     }
83   }
84   return true;
85 }
86 
main(int,char **)87 int main(int, char**) {
88   test();
89   static_assert(test());
90 
91   return 0;
92 }
93