| 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 <testsuite_hooks.h> |
| 20 | #include <generator> |
| 21 | |
| 22 | template<unsigned MaxCopies> |
| 23 | struct copy_max |
| 24 | { |
| 25 | int copy = 0; |
| 26 | |
| 27 | copy_max() |
| 28 | {} |
| 29 | |
| 30 | copy_max(const copy_max& o) |
| 31 | : copy {o.copy + 1} |
| 32 | { |
| 33 | VERIFY(copy <= MaxCopies); |
| 34 | } |
| 35 | |
| 36 | copy_max& |
| 37 | operator=(const copy_max& o) |
| 38 | { |
| 39 | copy = o.copy + 1; |
| 40 | VERIFY(copy <= MaxCopies); |
| 41 | return *this; |
| 42 | } |
| 43 | |
| 44 | copy_max(copy_max&& o) |
| 45 | { |
| 46 | std::swap(o.copy, this->copy); |
| 47 | } |
| 48 | |
| 49 | copy_max& |
| 50 | operator=(copy_max&& o) |
| 51 | { |
| 52 | std::swap(o.copy, this->copy); |
| 53 | return *this; |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | template<typename Ref> |
| 58 | std::generator<Ref> |
| 59 | foo() |
| 60 | { |
| 61 | co_yield {}; |
| 62 | } |
| 63 | |
| 64 | int |
| 65 | main() |
| 66 | { |
| 67 | static_assert(!std::copy_constructible<std::generator<int>>); |
| 68 | { |
| 69 | auto gen = foo<const copy_max<1>&>(); |
| 70 | auto i = gen.begin(); |
| 71 | *i; |
| 72 | *i; |
| 73 | auto is = *i; |
| 74 | VERIFY(is.copy > 0); |
| 75 | } |
| 76 | |
| 77 | { |
| 78 | auto gen2 = foo<copy_max<0>&&>(); |
| 79 | auto i = gen2.begin(); |
| 80 | *i; |
| 81 | *i; |
| 82 | auto is = *i; |
| 83 | } |
| 84 | |
| 85 | { |
| 86 | auto gen = foo<copy_max<0>>(); // should be same as case 2 |
| 87 | auto i = gen.begin(); |
| 88 | *i; |
| 89 | *i; |
| 90 | auto is = *i; |
| 91 | } |
| 92 | } |