| 1 |
// Copyright (C) 2019-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 <ranges> |
| 21 |
#include <testsuite_hooks.h> |
| 22 |
#include <testsuite_iterators.h> |
| 23 |
|
| 24 |
static_assert(__gnu_test::is_customization_point_object(std::ranges::empty)); |
| 25 |
|
| 26 |
using std::same_as; |
| 27 |
|
| 28 |
void |
| 29 |
test01() |
| 30 |
{ |
| 31 |
struct R |
| 32 |
{ |
| 33 |
constexpr int empty() const & { return 0; } |
| 34 |
constexpr const void* empty() const && { return this; } |
| 35 |
}; |
| 36 |
constexpr R r; |
| 37 |
static_assert( !std::ranges::empty(r) ); |
| 38 |
static_assert( same_as<decltype(std::ranges::empty(r)), bool> ); |
| 39 |
// PR libstdc++/100824 |
| 40 |
// ranges::empty should treat the subexpression as an lvalue |
| 41 |
static_assert( !std::ranges::empty(std::move(r)) ); |
| 42 |
static_assert( same_as<decltype(std::ranges::empty(std::move(r))), bool> ); |
| 43 |
} |
| 44 |
|
| 45 |
void |
| 46 |
test02() |
| 47 |
{ |
| 48 |
using __gnu_test::test_range; |
| 49 |
using __gnu_test::test_sized_range; |
| 50 |
using __gnu_test::random_access_iterator_wrapper; |
| 51 |
using __gnu_test::forward_iterator_wrapper; |
| 52 |
using __gnu_test::input_iterator_wrapper; |
| 53 |
using __gnu_test::output_iterator_wrapper; |
| 54 |
|
| 55 |
int a[] = { 0, 1 }; |
| 56 |
VERIFY( !std::ranges::empty(a) ); |
| 57 |
|
| 58 |
test_range<int, random_access_iterator_wrapper> r(a); |
| 59 |
VERIFY( !std::ranges::empty(r) ); |
| 60 |
|
| 61 |
test_range<int, forward_iterator_wrapper> i(a); |
| 62 |
VERIFY( !std::ranges::empty(i) ); |
| 63 |
|
| 64 |
test_sized_range<int, random_access_iterator_wrapper> sr(a); |
| 65 |
VERIFY( !std::ranges::empty(sr) ); |
| 66 |
|
| 67 |
test_sized_range<int, input_iterator_wrapper> si(a); |
| 68 |
VERIFY( !std::ranges::empty(si) ); |
| 69 |
|
| 70 |
test_sized_range<int, output_iterator_wrapper> so(a); |
| 71 |
VERIFY( !std::ranges::empty(so) ); |
| 72 |
} |
| 73 |
|
| 74 |
void |
| 75 |
test03() |
| 76 |
{ |
| 77 |
// PR libstdc++/100824 |
| 78 |
// ranges::empty should treat the subexpression as an lvalue |
| 79 |
|
| 80 |
struct R |
| 81 |
{ |
| 82 |
constexpr bool empty() & { return true; } |
| 83 |
}; |
| 84 |
static_assert( std::ranges::empty(R{}) ); |
| 85 |
|
| 86 |
struct R2 |
| 87 |
{ |
| 88 |
constexpr unsigned size() & { return 0; } |
| 89 |
}; |
| 90 |
static_assert( std::ranges::empty(R2{}) ); |
| 91 |
} |
| 92 |
|
| 93 |
void |
| 94 |
test04() |
| 95 |
{ |
| 96 |
struct E1 |
| 97 |
{ |
| 98 |
bool empty() const noexcept { return {}; } |
| 99 |
}; |
| 100 |
|
| 101 |
static_assert( noexcept(std::ranges::empty(E1{})) ); |
| 102 |
|
| 103 |
struct E2 |
| 104 |
{ |
| 105 |
bool empty() const noexcept(false) { return {}; } |
| 106 |
}; |
| 107 |
|
| 108 |
static_assert( ! noexcept(std::ranges::empty(E2{})) ); |
| 109 |
|
| 110 |
struct E3 |
| 111 |
{ |
| 112 |
struct B |
| 113 |
{ |
| 114 |
explicit operator bool() const noexcept(false) { return true; } |
| 115 |
}; |
| 116 |
|
| 117 |
B empty() const noexcept { return {}; } |
| 118 |
}; |
| 119 |
|
| 120 |
static_assert( ! noexcept(std::ranges::empty(E3{})) ); |
| 121 |
} |
| 122 |
|
| 123 |
template<typename T> |
| 124 |
concept has_empty = requires (T& t) { std::ranges::empty(t); }; |
| 125 |
|
| 126 |
// If T is an array of unknown bound, ranges::empty(E) is ill-formed. |
| 127 |
static_assert( ! has_empty<int[]> ); |
| 128 |
static_assert( ! has_empty<int(&)[]> ); |
| 129 |
static_assert( ! has_empty<int[][2]> ); |
| 130 |
struct Incomplete; |
| 131 |
static_assert( ! has_empty<Incomplete[]> ); |
| 132 |
|
| 133 |
int |
| 134 |
main() |
| 135 |
{ |
| 136 |
test01(); |
| 137 |
test02(); |
| 138 |
test03(); |
| 139 |
test04(); |
| 140 |
} |