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, c++20
10 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
11 
12 // friend constexpr auto iter_move(const iterator& i) noexcept(see below);
13 
14 #include <array>
15 #include <cassert>
16 #include <iterator>
17 #include <ranges>
18 #include <tuple>
19 
20 #include "../types.h"
21 
22 struct ThrowingMove {
23   ThrowingMove() = default;
ThrowingMoveThrowingMove24   ThrowingMove(ThrowingMove&&){};
25 };
26 
test()27 constexpr bool test() {
28   {
29     // underlying iter_move noexcept
30     std::array a1{1, 2, 3, 4};
31     const std::array a2{3.0, 4.0};
32 
33     std::ranges::zip_view v(a1, a2, std::views::iota(3L));
34     assert(std::ranges::iter_move(v.begin()) == std::make_tuple(1, 3.0, 3L));
35     static_assert(std::is_same_v<decltype(std::ranges::iter_move(v.begin())), std::tuple<int&&, const double&&, long>>);
36 
37     auto it = v.begin();
38     static_assert(noexcept(std::ranges::iter_move(it)));
39   }
40 
41   {
42     // underlying iter_move may throw
43     auto throwingMoveRange =
44         std::views::iota(0, 2) | std::views::transform([](auto) noexcept { return ThrowingMove{}; });
45     std::ranges::zip_view v(throwingMoveRange);
46     auto it = v.begin();
47     static_assert(!noexcept(std::ranges::iter_move(it)));
48   }
49 
50   {
51     // underlying iterators' iter_move are called through ranges::iter_move
52     adltest::IterMoveSwapRange r1{}, r2{};
53     assert(r1.iter_move_called_times == 0);
54     assert(r2.iter_move_called_times == 0);
55     std::ranges::zip_view v(r1, r2);
56     auto it = v.begin();
57     {
58       [[maybe_unused]] auto&& i = std::ranges::iter_move(it);
59       assert(r1.iter_move_called_times == 1);
60       assert(r2.iter_move_called_times == 1);
61     }
62     {
63       [[maybe_unused]] auto&& i = std::ranges::iter_move(it);
64       assert(r1.iter_move_called_times == 2);
65       assert(r2.iter_move_called_times == 2);
66     }
67   }
68 
69   return true;
70 }
71 
main(int,char **)72 int main(int, char**) {
73   test();
74   static_assert(test());
75 
76   return 0;
77 }
78