Debian Codegraph @

libstdc++-v3/testsuite/24_iterators/customization_points/iter_move.cc

raw
1 // Copyright (C) 2019-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do run { target c++20 } }
19
20 #include <iterator>
21 #include <testsuite_hooks.h>
22 #include <testsuite_iterators.h>
23
24 static_assert(__gnu_test::is_customization_point_object(std::ranges::iter_move));
25
26 struct X
27 {
28 int value;
29
30 constexpr X(int i) : value(i) { }
31
32 X(const X&) = default;
33 X& operator=(const X&) = default;
34
35 constexpr X(X&& x)
36 : value(x.value)
37 {
38 x.value = -2;
39 }
40
41 constexpr X& operator=(X&& x)
42 {
43 value = x.value;
44 x.value = -1;
45 return *this;
46 }
47 };
48
49 constexpr bool
50 test_X(int i, int j)
51 {
52 X x1{i}, x2{j};
53 (void) std::ranges::iter_move(&x1); // no-op
54 x1 = std::ranges::iter_move(&x2);
55 return x1.value == j && x2.value == -1;
56 }
57
58 static_assert( test_X(1, 2) );
59
60 void
61 test01()
62 {
63 VERIFY( test_X(3, 4) );
64 }
65
66 template<typename T>
67 using rval_ref = std::iter_rvalue_reference_t<T>;
68
69 static_assert(std::same_as<rval_ref<int*>, int&&>);
70 static_assert(std::same_as<rval_ref<const int*>, const int&&>);
71 static_assert(std::same_as<rval_ref<std::move_iterator<int*>>, int&&>);
72
73 template<typename T>
74 concept iter_movable = requires { std::ranges::iter_move(std::declval<T>()); };
75
76 struct Iter
77 {
78 friend int& iter_move(Iter&) { static int i = 1; return i; }
79 friend long iter_move(Iter&&) { return 2; }
80 const short& operator*() const & { static short s = 3; return s; }
81 friend float operator*(const Iter&&) { return 4.0f; }
82 };
83
84 void
85 test_adl()
86 {
87 Iter it;
88 const Iter& cit = it;
89
90 VERIFY( std::ranges::iter_move(it) == 1 );
91 VERIFY( std::ranges::iter_move(std::move(it)) == 2 );
92 VERIFY( std::ranges::iter_move(cit) == 3 );
93 VERIFY( std::ranges::iter_move(std::move(cit)) == 4.0f );
94
95 // The return type should be unchanged for ADL iter_move:
96 static_assert(std::same_as<decltype(std::ranges::iter_move(it)), int&>);
97 static_assert(std::same_as<decltype(std::ranges::iter_move(std::move(it))),
98 long>);
99 // When ADL iter_move is not used, return type should be an rvalue:
100 static_assert(std::same_as<decltype(std::ranges::iter_move(cit)),
101 const short&&>);
102 static_assert(std::same_as<decltype(std::ranges::iter_move(std::move(cit))),
103 float>);
104
105 // std::iter_rvalue_reference_t always considers the argument as lvalue.
106 static_assert(std::same_as<rval_ref<Iter>, int&>);
107 static_assert(std::same_as<rval_ref<Iter&>, int&>);
108 static_assert(std::same_as<rval_ref<const Iter>, const short&&>);
109 static_assert(std::same_as<rval_ref<const Iter&>, const short&&>);
110 }
111
112 void
113 test_pr106612()
114 {
115 // Bug 106612 ranges::iter_move does not consider iterator's value categories
116
117 struct I
118 {
119 int i{};
120 int& operator*() & { return i; }
121 int operator*() const & { return i; }
122 void operator*() && = delete;
123 };
124
125 static_assert( iter_movable<I&> );
126 static_assert( iter_movable<I const&> );
127 static_assert( ! iter_movable<I> );
128 static_assert( std::same_as<std::iter_rvalue_reference_t<I>, int&&> );
129 static_assert( std::same_as<std::iter_rvalue_reference_t<const I>, int> );
130
131 struct I2
132 {
133 int i{};
134 int& operator*() & { return i; }
135 int operator*() const & { return i; }
136 void operator*() &&;
137 };
138
139 static_assert( iter_movable<I2&> );
140 static_assert( iter_movable<I2 const&> );
141 static_assert( iter_movable<I2> );
142 static_assert( std::is_void_v<decltype(std::ranges::iter_move(I2{}))> );
143 static_assert( std::same_as<std::iter_rvalue_reference_t<I2>, int&&> );
144 static_assert( std::same_as<std::iter_rvalue_reference_t<I2 const>, int> );
145
146 enum E { e };
147 enum F { f };
148
149 struct I3
150 {
151 E operator*() const & { return e; }
152 F operator*() && { return f; }
153 };
154
155 static_assert( iter_movable<I3&> );
156 static_assert( iter_movable<I3> );
157 static_assert( std::same_as<decltype(std::ranges::iter_move(I3{})), F> );
158 }
159
160 void
161 test_pr119469()
162 {
163 // rvalue references to function types are weird.
164 using F = int();
165 static_assert( std::same_as<std::iter_rvalue_reference_t<F>, F&> );
166 static_assert( std::same_as<std::iter_rvalue_reference_t<F&>, F&> );
167 static_assert( std::same_as<std::iter_rvalue_reference_t<F&&>, F&> );
168 }
169
170 int
171 main()
172 {
173 test01();
174 test_adl();
175 test_pr119469();
176 }