Debian Codegraph @

libstdc++-v3/testsuite/std/ranges/adaptors/p2281.cc

raw
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 run { target c++20 } }
19
20 #include <algorithm>
21 #include <ranges>
22 #include <string>
23 #include <string_view>
24 #include <testsuite_hooks.h>
25
26 namespace ranges = std::ranges;
27 namespace views = std::ranges::views;
28
29 // Verify P2281 changes to the forwarding semantics of partial application
30 // and composition of range adaptor objects.
31
32 void
33 test01()
34 {
35 auto split_into_strings = [] (auto p) {
36 return views::lazy_split(p) | views::transform([](auto r){
37 return std::string(r.begin(), ranges::next(r.begin(), r.end()));
38 });
39 };
40 constexpr std::string_view s = "hello world";
41 constexpr std::string_view p = " ";
42 constexpr auto v1 = s | split_into_strings(p);
43 constexpr auto v2 = split_into_strings(p)(s);
44 VERIFY( ranges::equal(v1, (std::string_view[]){"hello", "world"}) );
45 VERIFY( ranges::equal(v2, (std::string_view[]){"hello", "world"}) );
46 }
47
48 struct move_only_range
49 {
50 move_only_range() { }
51 move_only_range(move_only_range&&);
52 move_only_range& operator=(move_only_range&&);
53 move_only_range(const move_only_range&) = delete;
54 move_only_range& operator=(const move_only_range&) = delete;
55 char* begin();
56 char* end();
57 };
58
59 template<>
60 inline constexpr bool std::ranges::enable_view<move_only_range> = true;
61
62 template<auto lazy_split = views::lazy_split>
63 void
64 test02()
65 {
66 std::string_view s;
67 move_only_range p;
68 static_assert(requires { s | lazy_split(std::move(p)); });
69 static_assert(requires { lazy_split(std::move(p))(s); });
70 static_assert(requires { lazy_split(std::move(p)) | views::all; });
71 static_assert(requires { views::all | lazy_split(std::move(p)); });
72 static_assert(!requires { lazy_split(p); });
73 static_assert(!requires { lazy_split(p) | views::all; });
74 static_assert(!requires { views::all | lazy_split(p); });
75 }
76
77 int
78 main()
79 {
80 test01();
81 test02();
82 }