// Copyright (C) 2019-2025 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the // Free Software Foundation; either version 3, or (at your option) // any later version. // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License along // with this library; see the file COPYING3. If not see // . // { dg-do run { target c++20 } } #include #include #include static_assert(__gnu_test::is_customization_point_object(std::ranges::iter_move)); struct X { int value; constexpr X(int i) : value(i) { } X(const X&) = default; X& operator=(const X&) = default; constexpr X(X&& x) : value(x.value) { x.value = -2; } constexpr X& operator=(X&& x) { value = x.value; x.value = -1; return *this; } }; constexpr bool test_X(int i, int j) { X x1{i}, x2{j}; (void) std::ranges::iter_move(&x1); // no-op x1 = std::ranges::iter_move(&x2); return x1.value == j && x2.value == -1; } static_assert( test_X(1, 2) ); void test01() { VERIFY( test_X(3, 4) ); } template using rval_ref = std::iter_rvalue_reference_t; static_assert(std::same_as, int&&>); static_assert(std::same_as, const int&&>); static_assert(std::same_as>, int&&>); template concept iter_movable = requires { std::ranges::iter_move(std::declval()); }; struct Iter { friend int& iter_move(Iter&) { static int i = 1; return i; } friend long iter_move(Iter&&) { return 2; } const short& operator*() const & { static short s = 3; return s; } friend float operator*(const Iter&&) { return 4.0f; } }; void test_adl() { Iter it; const Iter& cit = it; VERIFY( std::ranges::iter_move(it) == 1 ); VERIFY( std::ranges::iter_move(std::move(it)) == 2 ); VERIFY( std::ranges::iter_move(cit) == 3 ); VERIFY( std::ranges::iter_move(std::move(cit)) == 4.0f ); // The return type should be unchanged for ADL iter_move: static_assert(std::same_as); static_assert(std::same_as); // When ADL iter_move is not used, return type should be an rvalue: static_assert(std::same_as); static_assert(std::same_as); // std::iter_rvalue_reference_t always considers the argument as lvalue. static_assert(std::same_as, int&>); static_assert(std::same_as, int&>); static_assert(std::same_as, const short&&>); static_assert(std::same_as, const short&&>); } void test_pr106612() { // Bug 106612 ranges::iter_move does not consider iterator's value categories struct I { int i{}; int& operator*() & { return i; } int operator*() const & { return i; } void operator*() && = delete; }; static_assert( iter_movable ); static_assert( iter_movable ); static_assert( ! iter_movable ); static_assert( std::same_as, int&&> ); static_assert( std::same_as, int> ); struct I2 { int i{}; int& operator*() & { return i; } int operator*() const & { return i; } void operator*() &&; }; static_assert( iter_movable ); static_assert( iter_movable ); static_assert( iter_movable ); static_assert( std::is_void_v ); static_assert( std::same_as, int&&> ); static_assert( std::same_as, int> ); enum E { e }; enum F { f }; struct I3 { E operator*() const & { return e; } F operator*() && { return f; } }; static_assert( iter_movable ); static_assert( iter_movable ); static_assert( std::same_as ); } void test_pr119469() { // rvalue references to function types are weird. using F = int(); static_assert( std::same_as, F&> ); static_assert( std::same_as, F&> ); static_assert( std::same_as, F&> ); } int main() { test01(); test_adl(); test_pr119469(); }