libstdc++: Implement C++20 changes to insert iterators

std::insert_iterator and std::inserter need to be adjusted for C++20, so
that they use ranges::iterator_t. That alias template requires
ranges::begin to be defined. Rather than moving the whole of
ranges::begin (and related details like ranges::enable_borrowed_range)
into <iterator>, this defines a new, simpler version of ranges::begin
that is sufficient for ranges::iterator_t to be defined. This works
because ranges::iterator_t uses an lvalue reference type, so the logic
in ranges::begin for non-lvalue ranges (i.e. borrowed ranges) isn't
needed.

This also adds the missing constexpr specifiers to the other insert
iterators.

	* include/bits/iterator_concepts.h (__detail::__decay_copy)
	(__detail::__member_begin, __detail::__adl_begin): Move here from
	<bits/range_access.h>.
	(__detail::__ranges_begin, __detail::__range_iter_t): Define.
	* bits/range_access.h (__cust_access::__decay_copy)
	(__cust_access::__member_begin, __cust_access::__adl_begin): Move to
	<bits/iterator_concepts.h>.
	(ranges::iterator_t): Use __detail::__range_iter_t.
	* include/bits/stl_iterator.h (back_insert_iterator): Simplify
	conditional compilation. Add _GLIBCXX20_CONSTEXPR to all members.
	(front_insert_iterator): Likewise.
	(insert_iterator): Implement changes from P0896R4 for C++20.
	* testsuite/24_iterators/back_insert_iterator/constexpr.cc: New test.
	* testsuite/24_iterators/front_insert_iterator/constexpr.cc: New test.
	* testsuite/24_iterators/headers/iterator/synopsis_c++17.cc: Adjust
	for inclusion in synopsis_c++20.cc which expects different signatures
	for some function templates.
	* testsuite/24_iterators/insert_iterator/constexpr.cc: New test.
This commit is contained in:
Jonathan Wakely 2020-03-27 23:21:58 +00:00
parent 0302a2de7f
commit ae6076b5bc
8 changed files with 305 additions and 41 deletions

View File

@ -1,5 +1,24 @@
2020-03-27 Jonathan Wakely <jwakely@redhat.com>
* include/bits/iterator_concepts.h (__detail::__decay_copy)
(__detail::__member_begin, __detail::__adl_begin): Move here from
<bits/range_access.h>.
(__detail::__ranges_begin, __detail::__range_iter_t): Define.
* bits/range_access.h (__cust_access::__decay_copy)
(__cust_access::__member_begin, __cust_access::__adl_begin): Move to
<bits/iterator_concepts.h>.
(ranges::iterator_t): Use __detail::__range_iter_t.
* include/bits/stl_iterator.h (back_insert_iterator): Simplify
conditional compilation. Add _GLIBCXX20_CONSTEXPR to all members.
(front_insert_iterator): Likewise.
(insert_iterator): Implement changes from P0896R4 for C++20.
* testsuite/24_iterators/back_insert_iterator/constexpr.cc: New test.
* testsuite/24_iterators/front_insert_iterator/constexpr.cc: New test.
* testsuite/24_iterators/headers/iterator/synopsis_c++17.cc: Adjust
for inclusion in synopsis_c++20.cc which expects different signatures
for some function templates.
* testsuite/24_iterators/insert_iterator/constexpr.cc: New test.
* include/std/type_traits (__is_array_convertible): Move definition
to immediately after is_convertible.

View File

@ -835,6 +835,56 @@ namespace ranges
struct default_sentinel_t { };
inline constexpr default_sentinel_t default_sentinel{};
namespace __detail
{
template<typename _Tp>
constexpr decay_t<_Tp>
__decay_copy(_Tp&& __t)
noexcept(is_nothrow_convertible_v<_Tp, decay_t<_Tp>>)
{ return std::forward<_Tp>(__t); }
template<typename _Tp>
concept __member_begin = requires(_Tp& __t)
{
{ __detail::__decay_copy(__t.begin()) } -> input_or_output_iterator;
};
void begin(auto&) = delete;
void begin(const auto&) = delete;
template<typename _Tp>
concept __adl_begin = __class_or_enum<remove_reference_t<_Tp>>
&& requires(_Tp& __t)
{
{ __detail::__decay_copy(begin(__t)) } -> input_or_output_iterator;
};
// Simplified version of std::ranges::begin that only supports lvalues,
// for use by __range_iter_t below.
template<typename _Tp>
requires is_array_v<_Tp> || __member_begin<_Tp&> || __adl_begin<_Tp&>
auto
__ranges_begin(_Tp& __t)
{
if constexpr (is_array_v<_Tp>)
{
static_assert(sizeof(remove_all_extents_t<_Tp>) != 0,
"not array of incomplete type");
return __t + 0;
}
else if constexpr (__member_begin<_Tp&>)
return __t.begin();
else
return begin(__t);
}
// Implementation of std::ranges::iterator_t, without using ranges::begin.
template<typename _Tp>
using __range_iter_t
= decltype(__detail::__ranges_begin(std::declval<_Tp&>()));
} // namespace __detail
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
#endif // C++20 library concepts

View File

@ -372,28 +372,9 @@ namespace ranges
{
using std::ranges::__detail::__maybe_borrowed_range;
using std::__detail::__class_or_enum;
template<typename _Tp>
constexpr decay_t<_Tp>
__decay_copy(_Tp&& __t)
noexcept(is_nothrow_convertible_v<_Tp, decay_t<_Tp>>)
{ return std::forward<_Tp>(__t); }
template<typename _Tp>
concept __member_begin = requires(_Tp& __t)
{
{ __decay_copy(__t.begin()) } -> input_or_output_iterator;
};
void begin(auto&) = delete;
void begin(const auto&) = delete;
template<typename _Tp>
concept __adl_begin = __class_or_enum<remove_reference_t<_Tp>>
&& requires(_Tp& __t)
{
{ __decay_copy(begin(__t)) } -> input_or_output_iterator;
};
using std::__detail::__decay_copy;
using std::__detail::__member_begin;
using std::__detail::__adl_begin;
struct _Begin
{
@ -889,7 +870,7 @@ namespace ranges
= range<_Tp> && __detail::__maybe_borrowed_range<_Tp>;
template<typename _Tp>
using iterator_t = decltype(ranges::begin(std::declval<_Tp&>()));
using iterator_t = std::__detail::__range_iter_t<_Tp>;
template<range _Range>
using sentinel_t = decltype(ranges::end(std::declval<_Range&>()));

View File

@ -76,6 +76,7 @@
#if __cplusplus > 201703L
# include <compare>
# include <new>
# include <bits/iterator_concepts.h>
#endif
namespace std _GLIBCXX_VISIBILITY(default)
@ -496,25 +497,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public iterator<output_iterator_tag, void, void, void, void>
{
protected:
#if __cplusplus <= 201703L
_Container* container;
#else
_Container* container = nullptr;
#endif
public:
/// A nested typedef for the type of whatever container you used.
typedef _Container container_type;
#if __cplusplus > 201703L
using difference_type = ptrdiff_t;
#endif
#if __cplusplus > 201703L
constexpr back_insert_iterator() noexcept = default;
constexpr back_insert_iterator() noexcept : container(nullptr) { }
#endif
/// The only way to create this %iterator is with a container.
explicit
explicit _GLIBCXX20_CONSTEXPR
back_insert_iterator(_Container& __x)
: container(std::__addressof(__x)) { }
@ -537,6 +532,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
return *this;
}
#else
_GLIBCXX20_CONSTEXPR
back_insert_iterator&
operator=(const typename _Container::value_type& __value)
{
@ -544,6 +540,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
return *this;
}
_GLIBCXX20_CONSTEXPR
back_insert_iterator&
operator=(typename _Container::value_type&& __value)
{
@ -553,16 +550,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif
/// Simply returns *this.
_GLIBCXX20_CONSTEXPR
back_insert_iterator&
operator*()
{ return *this; }
/// Simply returns *this. (This %iterator does not @a move.)
_GLIBCXX20_CONSTEXPR
back_insert_iterator&
operator++()
{ return *this; }
/// Simply returns *this. (This %iterator does not @a move.)
_GLIBCXX20_CONSTEXPR
back_insert_iterator
operator++(int)
{ return *this; }
@ -580,6 +580,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* types for you.
*/
template<typename _Container>
_GLIBCXX20_CONSTEXPR
inline back_insert_iterator<_Container>
back_inserter(_Container& __x)
{ return back_insert_iterator<_Container>(__x); }
@ -599,25 +600,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public iterator<output_iterator_tag, void, void, void, void>
{
protected:
#if __cplusplus <= 201703L
_Container* container;
#else
_Container* container = nullptr;
#endif
public:
/// A nested typedef for the type of whatever container you used.
typedef _Container container_type;
#if __cplusplus > 201703L
using difference_type = ptrdiff_t;
#endif
#if __cplusplus > 201703L
constexpr front_insert_iterator() noexcept = default;
constexpr front_insert_iterator() noexcept : container(nullptr) { }
#endif
/// The only way to create this %iterator is with a container.
explicit front_insert_iterator(_Container& __x)
explicit _GLIBCXX20_CONSTEXPR
front_insert_iterator(_Container& __x)
: container(std::__addressof(__x)) { }
/**
@ -639,6 +635,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
return *this;
}
#else
_GLIBCXX20_CONSTEXPR
front_insert_iterator&
operator=(const typename _Container::value_type& __value)
{
@ -646,6 +643,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
return *this;
}
_GLIBCXX20_CONSTEXPR
front_insert_iterator&
operator=(typename _Container::value_type&& __value)
{
@ -655,16 +653,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif
/// Simply returns *this.
_GLIBCXX20_CONSTEXPR
front_insert_iterator&
operator*()
{ return *this; }
/// Simply returns *this. (This %iterator does not @a move.)
_GLIBCXX20_CONSTEXPR
front_insert_iterator&
operator++()
{ return *this; }
/// Simply returns *this. (This %iterator does not @a move.)
_GLIBCXX20_CONSTEXPR
front_insert_iterator
operator++(int)
{ return *this; }
@ -682,6 +683,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* types for you.
*/
template<typename _Container>
_GLIBCXX20_CONSTEXPR
inline front_insert_iterator<_Container>
front_inserter(_Container& __x)
{ return front_insert_iterator<_Container>(__x); }
@ -704,19 +706,36 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
class insert_iterator
: public iterator<output_iterator_tag, void, void, void, void>
{
#if __cplusplus > 201703L && defined __cpp_lib_concepts
using _Iter = std::__detail::__range_iter_t<_Container>;
protected:
_Container* container = nullptr;
_Iter iter = _Iter();
#else
typedef typename _Container::iterator _Iter;
protected:
_Container* container;
typename _Container::iterator iter;
_Iter iter;
#endif
public:
/// A nested typedef for the type of whatever container you used.
typedef _Container container_type;
#if __cplusplus > 201703L && defined __cpp_lib_concepts
using difference_type = ptrdiff_t;
insert_iterator() = default;
#endif
/**
* The only way to create this %iterator is with a container and an
* initial position (a normal %iterator into the container).
*/
insert_iterator(_Container& __x, typename _Container::iterator __i)
_GLIBCXX20_CONSTEXPR
insert_iterator(_Container& __x, _Iter __i)
: container(std::__addressof(__x)), iter(__i) {}
/**
@ -751,6 +770,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
return *this;
}
#else
_GLIBCXX20_CONSTEXPR
insert_iterator&
operator=(const typename _Container::value_type& __value)
{
@ -759,6 +779,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
return *this;
}
_GLIBCXX20_CONSTEXPR
insert_iterator&
operator=(typename _Container::value_type&& __value)
{
@ -769,16 +790,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif
/// Simply returns *this.
_GLIBCXX20_CONSTEXPR
insert_iterator&
operator*()
{ return *this; }
/// Simply returns *this. (This %iterator does not @a move.)
_GLIBCXX20_CONSTEXPR
insert_iterator&
operator++()
{ return *this; }
/// Simply returns *this. (This %iterator does not @a move.)
_GLIBCXX20_CONSTEXPR
insert_iterator&
operator++(int)
{ return *this; }
@ -796,6 +820,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
* template parameter deduction, making the compiler match the correct
* types for you.
*/
#if __cplusplus > 201703L && defined __cpp_lib_concepts
template<typename _Container>
constexpr insert_iterator<_Container>
inserter(_Container& __x, std::__detail::__range_iter_t<_Container> __i)
{ return insert_iterator<_Container>(__x, __i); }
#else
template<typename _Container, typename _Iterator>
inline insert_iterator<_Container>
inserter(_Container& __x, _Iterator __i)
@ -803,6 +833,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
return insert_iterator<_Container>(__x,
typename _Container::iterator(__i));
}
#endif
// @} group iterators

View File

@ -0,0 +1,54 @@
// Copyright (C) 2020 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
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }
#include <iterator>
struct container
{
using value_type = int;
constexpr int* begin() { return std::begin(data); }
constexpr int* end() { return next; }
constexpr void push_back(int val)
{
if (next == std::end(data))
throw val;
*next++ = val;
}
int data[3];
int* next = std::begin(data);
};
constexpr bool
test01()
{
container c;
std::back_insert_iterator<container> iter;
iter = std::back_inserter(c);
*iter++ = 1;
int i = 2;
*iter = i;
*++iter = 3;
return c.data[0] == 1 && c.data[1] == 2 && c.data[2] == 3;
}
static_assert( test01() );

View File

@ -0,0 +1,54 @@
// Copyright (C) 2020 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
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }
#include <iterator>
struct container
{
using value_type = int;
constexpr int* begin() { return next; }
constexpr int* end() { return std::end(data); }
constexpr void push_front(int val)
{
if (next == std::begin(data))
throw val;
*--next = val;
}
int data[3];
int* next = std::end(data);
};
constexpr bool
test01()
{
container c;
std::front_insert_iterator<container> iter;
iter = std::front_inserter(c);
*iter++ = 1;
int i = 2;
*iter = i;
*++iter = 3;
return c.data[0] == 3 && c.data[1] == 2 && c.data[2] == 1;
}
static_assert( test01() );

View File

@ -45,6 +45,10 @@ namespace std {
// C++17 24.5, iterator adaptors:
template <class Iterator> class reverse_iterator;
#if __cplusplus == 201703L
// These operators are constrained in C++20 mode and so don't match
// these signatures.
template <class Iterator1, class Iterator2>
constexpr
bool operator==(const reverse_iterator<Iterator1>& x,
@ -74,6 +78,7 @@ namespace std {
constexpr
bool operator<=(const reverse_iterator<Iterator1>& x,
const reverse_iterator<Iterator2>& y);
#endif // C++17
template <class Iterator1, class Iterator2>
constexpr auto
@ -92,18 +97,30 @@ namespace std {
template <class Container> class back_insert_iterator;
template <class Container>
#if __cplusplus > 201703L
constexpr
#endif
back_insert_iterator<Container> back_inserter(Container& x);
template <class Container> class front_insert_iterator;
template <class Container>
#if __cplusplus > 201703L
constexpr
#endif
front_insert_iterator<Container> front_inserter(Container& x);
template <class Container> class insert_iterator;
#if __cplusplus == 201703L
// This function template is defined differently in C++20 mode and so
// doesn't match this signature.
template <class Container, class Iterator>
insert_iterator<Container> inserter(Container& x, Iterator i);
// These operators are constrained in C++20 mode and so don't match
// these signatures.
template <class Iterator> class move_iterator;
template <class Iterator1, class Iterator2>
@ -135,6 +152,7 @@ namespace std {
constexpr
bool operator>=(const move_iterator<Iterator1>& x,
const move_iterator<Iterator2>& y);
#endif // C++17
template <class Iterator1, class Iterator2>
constexpr

View File

@ -0,0 +1,57 @@
// Copyright (C) 2020 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
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }
#include <iterator>
struct container
{
using value_type = int;
constexpr int* begin() { return std::begin(data); }
constexpr int* end() { return last; }
constexpr int* insert(int* pos, int val)
{
if (last == std::end(data))
throw val;
for (int* i = last++; i != pos; --i)
i[1] = i[0];
*pos = val;
return pos;
}
int data[3];
int* last = std::begin(data);
};
constexpr bool
test01()
{
container c;
std::insert_iterator<container> iter;
iter = std::inserter(c, c.begin());
*iter++ = 1;
int i = 2;
*iter = i;
*++iter = 3;
return c.data[0] == 1 && c.data[1] == 2 && c.data[2] == 3;
}
static_assert( test01() );