| 1 |
// Copyright (C) 2020-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 |
#include <iterator> |
| 21 |
|
| 22 |
struct move_only_iterator |
| 23 |
{ |
| 24 |
move_only_iterator() = default; |
| 25 |
move_only_iterator(move_only_iterator&&) = default; |
| 26 |
move_only_iterator& operator=(move_only_iterator&&) = default; |
| 27 |
|
| 28 |
move_only_iterator& operator++(); |
| 29 |
move_only_iterator operator++(int); |
| 30 |
int& operator*() const; |
| 31 |
|
| 32 |
bool operator==(const move_only_iterator&) const; |
| 33 |
}; |
| 34 |
|
| 35 |
template<> struct std::iterator_traits<move_only_iterator> |
| 36 |
{ |
| 37 |
using value_type = int; |
| 38 |
using difference_type = std::ptrdiff_t; |
| 39 |
using iterator_category = std::input_iterator_tag; |
| 40 |
}; |
| 41 |
|
| 42 |
static_assert(std::input_iterator<move_only_iterator>); |
| 43 |
|
| 44 |
template<typename T> |
| 45 |
concept has_member_base = requires (T t) { |
| 46 |
// LWG 3391 made the const& overload of move_iterator::base() |
| 47 |
// unconstrained and return a const reference. So rather than checking |
| 48 |
// whether base() is valid (which is now trivially true in an unevaluated |
| 49 |
// context), the below now checks whether decay-copying base() is valid. |
| 50 |
[](auto){}(std::forward<T>(t).base()); |
| 51 |
}; |
| 52 |
|
| 53 |
using move_only_move_iterator = std::move_iterator<move_only_iterator>; |
| 54 |
|
| 55 |
static_assert( ! has_member_base<move_only_move_iterator&> ); |
| 56 |
static_assert( ! has_member_base<const move_only_move_iterator&> ); |
| 57 |
static_assert( has_member_base<move_only_move_iterator> ); |
| 58 |
static_assert( ! has_member_base<const move_only_move_iterator> ); |
| 59 |
|
| 60 |
void |
| 61 |
test01() |
| 62 |
{ |
| 63 |
move_only_move_iterator m1 = std::make_move_iterator(move_only_iterator{}); |
| 64 |
move_only_move_iterator m2; |
| 65 |
m2 = std::move(m1); |
| 66 |
} |