| 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 <iterator> |
| 21 | #include <testsuite_hooks.h> |
| 22 | |
| 23 | void |
| 24 | test01() |
| 25 | { |
| 26 | using S = std::move_sentinel<const int*>; |
| 27 | using M = std::move_iterator<int*>; |
| 28 | |
| 29 | static_assert( std::is_default_constructible_v<S> ); |
| 30 | static_assert( std::is_copy_constructible_v<S> ); |
| 31 | static_assert( std::is_copy_assignable_v<S> ); |
| 32 | static_assert( std::is_constructible_v<S, std::move_sentinel<int*>> ); |
| 33 | static_assert( std::is_assignable_v<S, std::move_sentinel<int*>> ); |
| 34 | |
| 35 | constexpr S s; |
| 36 | static_assert( s.base() == nullptr ); |
| 37 | |
| 38 | constexpr M m; |
| 39 | static_assert( m == s ); |
| 40 | static_assert( s == m ); |
| 41 | static_assert( !(m != s) ); |
| 42 | static_assert( !(s != m) ); |
| 43 | |
| 44 | int i = 0; |
| 45 | M m2(&i); |
| 46 | VERIFY( m2 != s ); |
| 47 | VERIFY( s != m2 ); |
| 48 | VERIFY( !(m2 == s) ); |
| 49 | VERIFY( !(s == m2) ); |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | test02() |
| 54 | { |
| 55 | struct sentinel { int limit; }; |
| 56 | |
| 57 | struct iterator |
| 58 | { |
| 59 | using iterator_category = std::input_iterator_tag; |
| 60 | using value_type = int; |
| 61 | using difference_type = std::ptrdiff_t; |
| 62 | using reference = const int&; |
| 63 | |
| 64 | const int& operator*() const { return counter; } |
| 65 | |
| 66 | iterator& operator++() { ++counter; return *this; } |
| 67 | |
| 68 | iterator operator++(int) { auto i = *this; ++counter; return i; } |
| 69 | |
| 70 | bool operator==(sentinel s) const { return counter == s.limit; } |
| 71 | |
| 72 | int counter = 0; |
| 73 | }; |
| 74 | |
| 75 | static_assert( std::sentinel_for<sentinel, iterator> ); |
| 76 | |
| 77 | iterator i; |
| 78 | sentinel s{5}; |
| 79 | int count = 0; |
| 80 | for (auto m = std::make_move_iterator(i); m != std::move_sentinel{s}; ++m) |
| 81 | ++count; |
| 82 | VERIFY( count == 5 ); |
| 83 | } |
| 84 | |
| 85 | int |
| 86 | main() |
| 87 | { |
| 88 | test01(); |
| 89 | test02(); |
| 90 | } |