| 1 | // Copyright (C) 2021-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 compile { target c++20 } } |
| 19 | |
| 20 | // PR libstdc++/100577 |
| 21 | |
| 22 | #include <ranges> |
| 23 | #include <functional> |
| 24 | #if __STDC_HOSTED__ |
| 25 | #include <string> |
| 26 | #endif |
| 27 | |
| 28 | namespace ranges = std::ranges; |
| 29 | namespace views = std::ranges::views; |
| 30 | |
| 31 | void |
| 32 | test01() |
| 33 | { |
| 34 | // Verify adaptors are deemed to have simple extra arguments when appropriate. |
| 35 | using views::__adaptor::__adaptor_has_simple_extra_args; |
| 36 | using std::identity; |
| 37 | static_assert(__adaptor_has_simple_extra_args<decltype(views::transform), identity>); |
| 38 | static_assert(__adaptor_has_simple_extra_args<decltype(views::filter), identity>); |
| 39 | static_assert(__adaptor_has_simple_extra_args<decltype(views::drop), int>); |
| 40 | static_assert(__adaptor_has_simple_extra_args<decltype(views::take), int>); |
| 41 | static_assert(__adaptor_has_simple_extra_args<decltype(views::take_while), identity>); |
| 42 | static_assert(__adaptor_has_simple_extra_args<decltype(views::drop_while), identity>); |
| 43 | static_assert(__adaptor_has_simple_extra_args<decltype(views::lazy_split), char>); |
| 44 | #if __STDC_HOSTED__ |
| 45 | static_assert(__adaptor_has_simple_extra_args<decltype(views::lazy_split), std::string_view>); |
| 46 | static_assert(!__adaptor_has_simple_extra_args<decltype(views::lazy_split), std::string>); |
| 47 | #endif |
| 48 | |
| 49 | using views::__adaptor::__closure_has_simple_call_op; |
| 50 | __closure_has_simple_call_op auto a00 = views::all; |
| 51 | __closure_has_simple_call_op auto a01 = views::transform(std::identity{}); |
| 52 | __closure_has_simple_call_op auto a02 = views::filter(std::identity{}); |
| 53 | __closure_has_simple_call_op auto a03 = views::drop(42); |
| 54 | __closure_has_simple_call_op auto a04 = views::take(42); |
| 55 | __closure_has_simple_call_op auto a05 = views::take_while(std::identity{}); |
| 56 | __closure_has_simple_call_op auto a06 = views::drop_while(std::identity{}); |
| 57 | __closure_has_simple_call_op auto a07 = views::join; |
| 58 | __closure_has_simple_call_op auto a08 = views::common; |
| 59 | __closure_has_simple_call_op auto a09 = views::reverse; |
| 60 | __closure_has_simple_call_op auto a10 = views::keys; |
| 61 | __closure_has_simple_call_op auto a11 = views::lazy_split(' '); |
| 62 | __closure_has_simple_call_op auto a11a = views::split(' '); |
| 63 | // Verify composition of simple closures is simple. |
| 64 | __closure_has_simple_call_op auto b |
| 65 | = (a00 | a01) | (a02 | a03) | (a04 | a05 | a06) | (a07 | a08 | a09 | a10) | a11; |
| 66 | |
| 67 | #if __STDC_HOSTED__ |
| 68 | // Verify views::lazy_split(non_view_range) is an exception. |
| 69 | extern std::string s; |
| 70 | auto a12 = views::lazy_split(s); |
| 71 | static_assert(!__closure_has_simple_call_op<decltype(a12)>); |
| 72 | static_assert(!__closure_has_simple_call_op<decltype(a12 | a00)>); |
| 73 | static_assert(!__closure_has_simple_call_op<decltype(a00 | a12)>); |
| 74 | |
| 75 | // Likewise views::split(non_view_range). |
| 76 | auto a12a = views::split(s); |
| 77 | static_assert(!__closure_has_simple_call_op<decltype(a12a)>); |
| 78 | static_assert(!__closure_has_simple_call_op<decltype(a12a | a00)>); |
| 79 | static_assert(!__closure_has_simple_call_op<decltype(a00 | a12a)>); |
| 80 | #endif |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | test02() |
| 85 | { |
| 86 | // Range adaptor closures with a simple operator() aren't implemented using a |
| 87 | // fallback deleted overload, so when a call is ill-formed overload resolution |
| 88 | // fails. |
| 89 | extern int x[10]; |
| 90 | struct { } badarg; |
| 91 | views::transform(badarg)(x); // { dg-error "no match" } |
| 92 | views::filter(badarg)(x); // { dg-error "no match" } |
| 93 | views::take_while(badarg)(x); // { dg-error "no match" } |
| 94 | views::drop_while(badarg)(x); // { dg-error "no match" } |
| 95 | |
| 96 | (views::transform(badarg) | views::all)(x); // { dg-error "no match" } |
| 97 | (views::filter(badarg) | views::all)(x); // { dg-error "no match" } |
| 98 | (views::take_while(badarg) | views::all)(x); // { dg-error "no match" } |
| 99 | (views::drop_while(badarg) | views::all)(x); // { dg-error "no match" } |
| 100 | |
| 101 | // In C++20 mode, range adaptor closures with non-simple operator() are |
| 102 | // implemented using a fallback deleted overload, so when a call is |
| 103 | // ill-formed overload resolution succeeds but selects the deleted overload |
| 104 | // (but only when the closure is invoked as an rvalue). |
| 105 | views::lazy_split(badarg)(x); // { dg-error "deleted function|no match" } |
| 106 | (views::lazy_split(badarg) | views::all)(x); // { dg-error "deleted function|no match" } |
| 107 | auto a0 = views::lazy_split(badarg); |
| 108 | a0(x); // { dg-error "no match" }; |
| 109 | auto a1 = a0 | views::all; |
| 110 | a1(x); // { dg-error "no match" } |
| 111 | |
| 112 | views::split(badarg)(x); // { dg-error "deleted function|no match" } |
| 113 | (views::split(badarg) | views::all)(x); // { dg-error "deleted function|no match" } |
| 114 | auto a0a = views::split(badarg); |
| 115 | a0a(x); // { dg-error "no match" }; |
| 116 | auto a1a = a0a | views::all; |
| 117 | a1a(x); // { dg-error "no match" } |
| 118 | |
| 119 | views::take(badarg)(x); // { dg-error "deleted|no match" } |
| 120 | views::drop(badarg)(x); // { dg-error "deleted|no match" } |
| 121 | (views::take(badarg) | views::all)(x); // { dg-error "deleted|no match" } |
| 122 | (views::drop(badarg) | views::all)(x); // { dg-error "deleted|no match" } |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | test03() |
| 127 | { |
| 128 | // PR libstdc++/100940 |
| 129 | extern int x[10]; |
| 130 | struct S { operator int() && { return 5; }; }; |
| 131 | x | std::views::take(S{}); |
| 132 | x | std::views::drop(S{}); |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | test04() |
| 137 | { |
| 138 | #if __STDC_HOSTED__ |
| 139 | // Non-trivially-copyable extra arguments make a closure not simple. |
| 140 | using F = std::function<bool(bool)>; |
| 141 | static_assert(!std::is_trivially_copyable_v<F>); |
| 142 | using views::__adaptor::__closure_has_simple_call_op; |
| 143 | static_assert(!__closure_has_simple_call_op<decltype(views::take_while(std::declval<F>()))>); |
| 144 | static_assert(!__closure_has_simple_call_op<decltype(views::drop_while(std::declval<F>()))>); |
| 145 | static_assert(!__closure_has_simple_call_op<decltype(views::filter(std::declval<F>()))>); |
| 146 | static_assert(!__closure_has_simple_call_op<decltype(views::transform(std::declval<F>()))>); |
| 147 | #endif |
| 148 | } |
| 149 | |
| 150 | // { dg-prune-output "in requirements" } |