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 constexpr void iter_swap(const iterator& x, const iterator& y);
12
13#include <cassert>
14#include <ranges>
15
16#include "../types.h"
17
18using NonSwappableView = BufferView<copying_iterator<int*>>;
19static_assert(std::ranges::input_range<NonSwappableView>);
20static_assert(!std::indirectly_swappable<std::ranges::iterator_t<NonSwappableView>>);
21
22constexpr bool test() {
23 int buffer[4][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
24
25 {
26 std::ranges::join_view jv(buffer);
27 auto iter1 = jv.begin();
28 auto iter2 = std::next(jv.begin());
29 assert(buffer[0][0] == 1);
30 assert(buffer[0][1] == 2);
31 std::ranges::iter_swap(iter1, iter2);
32 assert(buffer[0][0] == 2);
33 assert(buffer[0][1] == 1);
34
35 static_assert(noexcept(std::ranges::iter_swap(iter1, iter2)));
36 }
37
38 {
39 // iter_move calls inner's iter_swap
40 IterMoveSwapAwareView inners[1] = {buffer[0]};
41 std::ranges::join_view jv(inners);
42 auto it1 = jv.begin();
43 auto it2 = std::ranges::next(it1);
44
45 const auto& iter_swap_called_times = jv.base().begin()->iter_swap_called;
46
47 assert(iter_swap_called_times == 0);
48 assert(buffer[0][0] == 2);
49 assert(buffer[0][1] == 1);
50
51 std::ranges::iter_swap(it1, it2);
52
53 assert(buffer[0][0] == 1);
54 assert(buffer[0][1] == 2);
55 assert(iter_swap_called_times == 1);
56 }
57
58 {
59 // LWG3517 `join_view::iterator`'s `iter_swap` is underconstrained
60 // `iter_swap` should not be defined if Inner's iterator does not indirectly_swappable
61 NonSwappableView inners[2] = {buffer[0], buffer[1]};
62 std::ranges::join_view jv(inners);
63 using Iter = std::ranges::iterator_t<decltype(jv)>;
64 static_assert(!std::indirectly_swappable<Iter>);
65 }
66 return true;
67}
68
69int main(int, char**) {
70 test();
71 static_assert(test());
72
73 return 0;
74}
75

source code of libcxx/test/std/ranges/range.adaptors/range.join/range.join.iterator/iter.swap.pass.cpp