]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/std/ranges/adaptors/p2281.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / std / ranges / adaptors / p2281.cc
CommitLineData
a945c346 1// Copyright (C) 2021-2024 Free Software Foundation, Inc.
a25321ca
PP
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
d4ac20b0 18// { dg-do run { target c++20 } }
a25321ca
PP
19
20#include <algorithm>
21#include <ranges>
22#include <string>
23#include <string_view>
24#include <testsuite_hooks.h>
25
26namespace ranges = std::ranges;
27namespace views = std::ranges::views;
28
29// Verify P2281 changes to the forwarding semantics of partial application
30// and composition of range adaptor objects.
31
32void
33test01()
34{
35 auto split_into_strings = [] (auto p) {
adbd2c71 36 return views::lazy_split(p) | views::transform([](auto r){
a25321ca
PP
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
48struct 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
59template<>
60 inline constexpr bool std::ranges::enable_view<move_only_range> = true;
61
adbd2c71 62template<auto lazy_split = views::lazy_split>
a25321ca
PP
63void
64test02()
65{
66 std::string_view s;
67 move_only_range p;
adbd2c71
PP
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); });
a25321ca
PP
75}
76
77int
78main()
79{
80 test01();
81 test02();
82}