// 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
void
test01()
{
using I = std::common_iterator;
static_assert( std::is_default_constructible_v );
static_assert( std::is_copy_constructible_v );
static_assert( std::is_move_constructible_v );
static_assert( std::is_copy_assignable_v );
static_assert( std::is_move_assignable_v );
static_assert( std::is_constructible_v );
static_assert( std::is_constructible_v );
static_assert( std::is_nothrow_copy_constructible_v ); // GCC extension
static_assert( std::is_nothrow_move_constructible_v ); // GCC extension
static_assert( std::is_nothrow_copy_assignable_v ); // GCC extension
static_assert( std::is_nothrow_move_assignable_v ); // GCC extension
struct sentinel { operator int*() const noexcept { return nullptr; } };
using K = std::common_iterator;
static_assert( std::is_constructible_v );
static_assert( std::is_assignable_v );
static_assert( std::is_nothrow_assignable_v ); // GCC extension
struct sentinel_throwing { operator int*() const { return nullptr; } };
using K_throwing = std::common_iterator;
// Conversion is noexcept(false)
static_assert( ! std::is_nothrow_assignable_v );
struct sentinel2
{
const int* p;
sentinel2(const int* p = 0) : p(p) { }
bool operator==(const int* p) const { return p == this->p; }
};
using J = std::common_iterator;
static_assert( std::is_constructible_v );
static_assert( std::is_convertible_v );
static_assert( std::is_constructible_v );
static_assert( std::is_convertible_v );
// Constructor is noexcept(false)
static_assert( ! std::is_nothrow_constructible_v );
}
void
test02()
{
struct sentinel { int limit; };
struct iterator
{
using iterator_category = std::input_iterator_tag;
using value_type = int;
using difference_type = std::ptrdiff_t;
using reference = const int&;
const int& operator*() const { return counter; }
iterator& operator++() { ++counter; return *this; }
iterator operator++(int) { auto i = *this; ++counter; return i; }
bool operator==(sentinel s) const { return counter == s.limit; }
int counter = 0;
};
static_assert( std::sentinel_for );
int out[5] = { };
std::common_iterator obegin = std::begin(out);
std::common_iterator oend = std::cend(out);
iterator i;
sentinel s{5};
std::common_iterator begin = i, end = s;
while (begin != end)
*obegin++ = *begin++;
VERIFY(obegin == oend);
for (int& i : out)
VERIFY( i == (&i - out) );
}
constexpr bool
test03()
{
int arr[2] = { 1, 2 };
std::common_iterator i = std::ranges::begin(arr);
std::common_iterator end = std::ranges::cend(arr);
VERIFY( i != end );
VERIFY( (end - i) == 2 );
VERIFY( (i - end) == -2 );
auto j = i;
VERIFY( j == i );
VERIFY( (j - i) == 0 );
j = end;
VERIFY( j != i );
VERIFY( j == end );
j = std::ranges::next(i);
VERIFY( j != i );
VERIFY( j != end );
VERIFY( (end - j) == 1 );
VERIFY( (j - i) == 1 );
VERIFY( (i - j) == -1 );
++j;
VERIFY( j == end );
VERIFY( (end - j) == 0 );
j = i;
VERIFY( j == i );
VERIFY( (j - end) == -2 );
VERIFY( (j - i) == 0 );
if (std::is_constant_evaluated())
return true;
try
{
struct S { operator const int*() const { throw 1; } };
i = std::common_iterator(S{});
VERIFY( false );
}
catch (int)
{
}
return true;
}
static_assert( test03() );
void
test04()
{
struct X
{
X(int i) : i(i) { }
X(X&& x) : i(x.i) { x.i = -1; }
X& operator=(X&& x) { i = x.i; x.i = 0; return *this; }
int i;
};
X arr[] = { 1, 2 };
std::common_iterator i(arr), j(arr+1);
std::ranges::iter_swap(i, j);
VERIFY( arr[0].i == 2 );
VERIFY( arr[1].i == 1 );
X x = std::ranges::iter_move(i);
VERIFY( arr[0].i == -1 );
VERIFY( x.i == 2 );
}
constexpr bool
test_pr103992()
{
using C1 = std::common_iterator,
std::unreachable_sentinel_t>;
using C2 = std::common_iterator,
std::unreachable_sentinel_t>;
C1 c1;
C2 c2 = c1;
C1 c3 = c1;
return true;
}
static_assert( test_pr103992() );
int
main()
{
test01();
test02();
test03();
test04();
}