| 1 |
// { dg-do run { target c++23 } } |
| 2 |
// Copyright (C) 2023-2025 Free Software Foundation, Inc. |
| 3 |
// |
| 4 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 5 |
// software; you can redistribute it and/or modify it under the |
| 6 |
// terms of the GNU General Public License as published by the |
| 7 |
// Free Software Foundation; either version 3, or (at your option) |
| 8 |
// any later version. |
| 9 |
|
| 10 |
// This library is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU General Public License for more details. |
| 14 |
|
| 15 |
// You should have received a copy of the GNU General Public License along |
| 16 |
// with this library; see the file COPYING3. If not see |
| 17 |
// <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
#include <iostream> |
| 20 |
#include <generator> |
| 21 |
#include <testsuite_hooks.h> |
| 22 |
|
| 23 |
struct foo |
| 24 |
{ |
| 25 |
int id; |
| 26 |
|
| 27 |
foo(int id) |
| 28 |
: id { id } |
| 29 |
{} |
| 30 |
|
| 31 |
foo(const foo& o) |
| 32 |
: id { o.id * 100 } |
| 33 |
{ |
| 34 |
std::cout << "copy-consed " << o.id << "->" << id << '\n'; |
| 35 |
} |
| 36 |
|
| 37 |
foo& |
| 38 |
operator=(const foo& o) |
| 39 |
{ |
| 40 |
id = o.id * 100; |
| 41 |
std::cout << "copied " << o.id << "->" << id << '\n'; |
| 42 |
return *this; |
| 43 |
} |
| 44 |
|
| 45 |
foo(foo&& o) |
| 46 |
: id { o.id } |
| 47 |
{ |
| 48 |
o.id = -1; |
| 49 |
std::cout << "moved " << id << '\n'; |
| 50 |
} |
| 51 |
|
| 52 |
foo& |
| 53 |
operator=(foo&& o) |
| 54 |
{ |
| 55 |
std::swap(o.id, id); |
| 56 |
std::cout << "swapped " << id << '\n'; |
| 57 |
return *this; |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
std::generator<foo> |
| 62 |
foogen() |
| 63 |
{ |
| 64 |
co_yield foo{0}; |
| 65 |
|
| 66 |
{ |
| 67 |
foo f {1}; |
| 68 |
co_yield f; |
| 69 |
} |
| 70 |
|
| 71 |
{ |
| 72 |
const foo f {2}; |
| 73 |
co_yield f; |
| 74 |
} |
| 75 |
|
| 76 |
{ |
| 77 |
foo f {3}; |
| 78 |
co_yield std::move(f); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
std::generator<const foo&> |
| 83 |
foogen2() |
| 84 |
{ |
| 85 |
co_yield foo{0}; |
| 86 |
|
| 87 |
{ |
| 88 |
foo f {1}; |
| 89 |
co_yield f; |
| 90 |
} |
| 91 |
|
| 92 |
{ |
| 93 |
const foo f {2}; |
| 94 |
co_yield f; |
| 95 |
} |
| 96 |
|
| 97 |
{ |
| 98 |
foo f {3}; |
| 99 |
co_yield std::move(f); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
std::generator<foo&&> |
| 104 |
foogen3() |
| 105 |
{ |
| 106 |
co_yield foo{0}; |
| 107 |
|
| 108 |
{ |
| 109 |
foo f {1}; |
| 110 |
co_yield f; |
| 111 |
} |
| 112 |
|
| 113 |
{ |
| 114 |
const foo f {2}; |
| 115 |
co_yield f; |
| 116 |
} |
| 117 |
|
| 118 |
{ |
| 119 |
foo f {3}; |
| 120 |
co_yield std::move(f); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
int |
| 125 |
main() |
| 126 |
{ |
| 127 |
for (auto f : foogen()) |
| 128 |
std::cout << f.id << '\n'; |
| 129 |
for (const auto& f : foogen()) |
| 130 |
std::cout << f.id << '\n'; |
| 131 |
for (auto&& f : foogen()) |
| 132 |
std::cout << f.id << '\n'; |
| 133 |
|
| 134 |
std::cout << "---\n"; |
| 135 |
|
| 136 |
for (auto f : foogen2()) |
| 137 |
std::cout << f.id << '\n'; |
| 138 |
for (const auto& f : foogen2()) |
| 139 |
std::cout << f.id << '\n'; |
| 140 |
for (auto&& f : foogen2()) |
| 141 |
std::cout << f.id << '\n'; |
| 142 |
|
| 143 |
std::cout << "---\n"; |
| 144 |
|
| 145 |
for (auto f : foogen3()) |
| 146 |
std::cout << f.id << '\n'; |
| 147 |
for (const auto& f : foogen3()) |
| 148 |
std::cout << f.id << '\n'; |
| 149 |
for (auto&& f : foogen3()) |
| 150 |
std::cout << f.id << '\n'; |
| 151 |
} |
| 152 |
|
| 153 |
// { dg-output {moved 0(\n|\r\n|\r)} } |
| 154 |
// { dg-output {0(\n|\r\n|\r)} } |
| 155 |
// { dg-output {copy-consed 1->100(\n|\r\n|\r)} } |
| 156 |
// { dg-output {moved 100(\n|\r\n|\r)} } |
| 157 |
// { dg-output {100(\n|\r\n|\r)} } |
| 158 |
// { dg-output {copy-consed 2->200(\n|\r\n|\r)} } |
| 159 |
// { dg-output {moved 200(\n|\r\n|\r)} } |
| 160 |
// { dg-output {200(\n|\r\n|\r)} } |
| 161 |
// { dg-output {moved 3(\n|\r\n|\r)} } |
| 162 |
// { dg-output {3(\n|\r\n|\r)} } |
| 163 |
// { dg-output {0(\n|\r\n|\r)} } |
| 164 |
// { dg-output {copy-consed 1->100(\n|\r\n|\r)} } |
| 165 |
// { dg-output {100(\n|\r\n|\r)} } |
| 166 |
// { dg-output {copy-consed 2->200(\n|\r\n|\r)} } |
| 167 |
// { dg-output {200(\n|\r\n|\r)} } |
| 168 |
// { dg-output {3(\n|\r\n|\r)} } |
| 169 |
// { dg-output {0(\n|\r\n|\r)} } |
| 170 |
// { dg-output {copy-consed 1->100(\n|\r\n|\r)} } |
| 171 |
// { dg-output {100(\n|\r\n|\r)} } |
| 172 |
// { dg-output {copy-consed 2->200(\n|\r\n|\r)} } |
| 173 |
// { dg-output {200(\n|\r\n|\r)} } |
| 174 |
// { dg-output {3(\n|\r\n|\r)} } |
| 175 |
// { dg-output {---(\n|\r\n|\r)} } |
| 176 |
// { dg-output {copy-consed 0->0(\n|\r\n|\r)} } |
| 177 |
// { dg-output {0(\n|\r\n|\r)} } |
| 178 |
// { dg-output {copy-consed 1->100(\n|\r\n|\r)} } |
| 179 |
// { dg-output {100(\n|\r\n|\r)} } |
| 180 |
// { dg-output {copy-consed 2->200(\n|\r\n|\r)} } |
| 181 |
// { dg-output {200(\n|\r\n|\r)} } |
| 182 |
// { dg-output {copy-consed 3->300(\n|\r\n|\r)} } |
| 183 |
// { dg-output {300(\n|\r\n|\r)} } |
| 184 |
// { dg-output {0(\n|\r\n|\r)} } |
| 185 |
// { dg-output {1(\n|\r\n|\r)} } |
| 186 |
// { dg-output {2(\n|\r\n|\r)} } |
| 187 |
// { dg-output {3(\n|\r\n|\r)} } |
| 188 |
// { dg-output {0(\n|\r\n|\r)} } |
| 189 |
// { dg-output {1(\n|\r\n|\r)} } |
| 190 |
// { dg-output {2(\n|\r\n|\r)} } |
| 191 |
// { dg-output {3(\n|\r\n|\r)} } |
| 192 |
// { dg-output {---(\n|\r\n|\r)} } |
| 193 |
// { dg-output {moved 0(\n|\r\n|\r)} } |
| 194 |
// { dg-output {0(\n|\r\n|\r)} } |
| 195 |
// { dg-output {copy-consed 1->100(\n|\r\n|\r)} } |
| 196 |
// { dg-output {moved 100(\n|\r\n|\r)} } |
| 197 |
// { dg-output {100(\n|\r\n|\r)} } |
| 198 |
// { dg-output {copy-consed 2->200(\n|\r\n|\r)} } |
| 199 |
// { dg-output {moved 200(\n|\r\n|\r)} } |
| 200 |
// { dg-output {200(\n|\r\n|\r)} } |
| 201 |
// { dg-output {moved 3(\n|\r\n|\r)} } |
| 202 |
// { dg-output {3(\n|\r\n|\r)} } |
| 203 |
// { dg-output {0(\n|\r\n|\r)} } |
| 204 |
// { dg-output {copy-consed 1->100(\n|\r\n|\r)} } |
| 205 |
// { dg-output {100(\n|\r\n|\r)} } |
| 206 |
// { dg-output {copy-consed 2->200(\n|\r\n|\r)} } |
| 207 |
// { dg-output {200(\n|\r\n|\r)} } |
| 208 |
// { dg-output {3(\n|\r\n|\r)} } |
| 209 |
// { dg-output {0(\n|\r\n|\r)} } |
| 210 |
// { dg-output {copy-consed 1->100(\n|\r\n|\r)} } |
| 211 |
// { dg-output {100(\n|\r\n|\r)} } |
| 212 |
// { dg-output {copy-consed 2->200(\n|\r\n|\r)} } |
| 213 |
// { dg-output {200(\n|\r\n|\r)} } |
| 214 |
// { dg-output {3(\n|\r\n|\r)} } |