Skip to content

Commit 0473318

Browse files
committed
[libcxx][iterator] adds std::indirectly_readable and std::indirectly_writable
Implements parts of: * P0896R4 The One Ranges Proposal` Depends on D99873. Reviewed By: ldionne, #libc Differential Revision: https://reviews.llvm.org/D100073
1 parent 89b5934 commit 0473318

File tree

41 files changed

+1151
-13
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1151
-13
lines changed

libcxx/include/__iterator/concepts.h

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
#include <__config>
1414
#include <concepts>
15+
#include <__iterator/iter_move.h>
16+
#include <__iterator/incrementable_traits.h>
17+
#include <__iterator/iterator_traits.h>
18+
#include <__iterator/readable_traits.h>
1519

1620
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1721
#pragma GCC system_header
@@ -24,18 +28,36 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2428

2529
#if !defined(_LIBCPP_HAS_NO_RANGES)
2630

27-
template<class _Tp>
28-
using __with_reference = _Tp&;
31+
// clang-format off
2932

30-
template<class _Tp>
31-
concept __referenceable = requires {
32-
typename __with_reference<_Tp>;
33-
};
33+
// [iterator.concept.readable]
34+
template<class _In>
35+
concept __indirectly_readable_impl =
36+
requires(const _In __in) {
37+
typename iter_value_t<_In>;
38+
typename iter_reference_t<_In>;
39+
typename iter_rvalue_reference_t<_In>;
40+
{ *__in } -> same_as<iter_reference_t<_In> >;
41+
{ ranges::iter_move(__in) } -> same_as<iter_rvalue_reference_t<_In> >;
42+
} &&
43+
common_reference_with<iter_reference_t<_In>&&, iter_value_t<_In>&> &&
44+
common_reference_with<iter_reference_t<_In>&&, iter_rvalue_reference_t<_In>&&> &&
45+
common_reference_with<iter_rvalue_reference_t<_In>&&, const iter_value_t<_In>&>;
3446

35-
template<class _Tp>
36-
concept __dereferenceable = requires(_Tp& __t) {
37-
{ *__t } -> __referenceable; // not required to be equality-preserving
38-
};
47+
template<class _In>
48+
concept indirectly_readable = __indirectly_readable_impl<remove_cvref_t<_In> >;
49+
50+
// [iterator.concept.writable]
51+
template<class _Out, class _Tp>
52+
concept indirectly_writable =
53+
requires(_Out&& __o, _Tp&& __t) {
54+
*__o = _VSTD::forward<_Tp>(__t); // not required to be equality-preserving
55+
*_VSTD::forward<_Out>(__o) = _VSTD::forward<_Tp>(__t); // not required to be equality-preserving
56+
const_cast<const iter_reference_t<_Out>&&>(*__o) = _VSTD::forward<_Tp>(__t); // not required to be equality-preserving
57+
const_cast<const iter_reference_t<_Out>&&>(*_VSTD::forward<_Out>(__o)) = _VSTD::forward<_Tp>(__t); // not required to be equality-preserving
58+
};
59+
60+
// clang-format on
3961

4062
#endif // !defined(_LIBCPP_HAS_NO_RANGES)
4163

libcxx/include/__iterator/incrementable_traits.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define _LIBCPP___ITERATOR_INCREMENTABLE_TRAITS_H
1212

1313
#include <__config>
14-
#include <__iterator/concepts.h>
14+
#include <concepts>
1515
#include <type_traits>
1616

1717
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)

libcxx/include/__iterator/iter_move.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define _LIBCPP___ITERATOR_ITER_MOVE_H
1212

1313
#include <__config>
14-
#include <__iterator/concepts.h>
14+
#include <__iterator/iterator_traits.h>
1515
#include <concepts> // __class_or_enum
1616
#include <type_traits>
1717
#include <utility>

libcxx/include/__iterator/iterator_traits.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,23 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2727

2828
#if !defined(_LIBCPP_HAS_NO_RANGES)
2929

30+
template <class _Tp>
31+
using __with_reference = _Tp&;
32+
33+
template <class _Tp>
34+
concept __referenceable = requires {
35+
typename __with_reference<_Tp>;
36+
};
37+
38+
template <class _Tp>
39+
concept __dereferenceable = requires(_Tp& __t) {
40+
{ *__t } -> __referenceable; // not required to be equality-preserving
41+
};
42+
3043
// [iterator.traits]
3144
template<__dereferenceable _Tp>
3245
using iter_reference_t = decltype(*declval<_Tp&>());
46+
3347
#endif // !defined(_LIBCPP_HAS_NO_RANGES)
3448

3549
template <class _Iter>

libcxx/include/__iterator/readable_traits.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
#define _LIBCPP___ITERATOR_READABLE_TRAITS_H
1212

1313
#include <__config>
14-
#include <__iterator/concepts.h>
14+
#include <concepts>
15+
#include <type_traits>
1516

1617
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1718
#pragma GCC system_header

libcxx/include/iterator

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ template<dereferenceable T>
4343
requires ...
4444
using iter_rvalue_reference_t = decltype(ranges::iter_move(declval<T&>())); // since C++20
4545
46+
// [iterator.concepts], iterator concepts
47+
// [iterator.concept.readable], concept indirectly_­readable
48+
template<class In>
49+
concept indirectly_readable = see below; // since C++20
50+
51+
// [iterator.concept.writable], concept indirectly_­writable
52+
template<class Out, class T>
53+
concept indirectly_writable = see below; // since C++20
54+
4655
template<class Category, class T, class Distance = ptrdiff_t,
4756
class Pointer = T*, class Reference = T&>
4857
struct iterator
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
// UNSUPPORTED: libcpp-no-concepts
11+
// UNSUPPORTED: gcc-10
12+
13+
// iterator, const_iterator, reverse_iterator, const_reverse_iterator
14+
15+
#include <map>
16+
17+
#include <iterator>
18+
19+
using iterator = std::map<int, int>::iterator;
20+
using const_iterator = std::map<int, int>::const_iterator;
21+
using value_type = iterator::value_type;
22+
23+
static_assert(std::indirectly_readable<iterator>);
24+
static_assert(!std::indirectly_writable<iterator, value_type>);
25+
26+
static_assert(std::indirectly_readable<const_iterator>);
27+
static_assert(!std::indirectly_writable<const_iterator, value_type>);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
// UNSUPPORTED: libcpp-no-concepts
11+
// UNSUPPORTED: gcc-10
12+
13+
// iterator, const_iterator, reverse_iterator, const_reverse_iterator
14+
15+
#include <map>
16+
17+
#include <iterator>
18+
19+
using iterator = std::multimap<int, int>::iterator;
20+
using const_iterator = std::multimap<int, int>::const_iterator;
21+
using value_type = iterator::value_type;
22+
23+
static_assert(std::indirectly_readable<iterator>);
24+
static_assert(!std::indirectly_writable<iterator, value_type>);
25+
26+
static_assert(std::indirectly_readable<const_iterator>);
27+
static_assert(!std::indirectly_writable<const_iterator, value_type>);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
// UNSUPPORTED: libcpp-no-concepts
11+
// UNSUPPORTED: gcc-10
12+
13+
// iterator, const_iterator, reverse_iterator, const_reverse_iterator
14+
15+
#include <set>
16+
17+
#include <iterator>
18+
19+
using iterator = std::multiset<int>::iterator;
20+
using const_iterator = std::multiset<int>::const_iterator;
21+
using value_type = iterator::value_type;
22+
23+
static_assert(std::indirectly_readable<iterator>);
24+
static_assert(!std::indirectly_writable<iterator, value_type>);
25+
26+
static_assert(std::indirectly_readable<const_iterator>);
27+
static_assert(!std::indirectly_writable<const_iterator, value_type>);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
// UNSUPPORTED: libcpp-no-concepts
11+
// UNSUPPORTED: gcc-10
12+
13+
// iterator, const_iterator, reverse_iterator, const_reverse_iterator
14+
15+
#include <set>
16+
17+
#include <iterator>
18+
19+
using iterator = std::set<int>::iterator;
20+
using const_iterator = std::set<int>::const_iterator;
21+
using value_type = iterator::value_type;
22+
23+
static_assert(std::indirectly_readable<iterator>);
24+
static_assert(!std::indirectly_writable<iterator, value_type>);
25+
26+
static_assert(std::indirectly_readable<const_iterator>);
27+
static_assert(!std::indirectly_writable<const_iterator, value_type>);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
// UNSUPPORTED: libcpp-no-concepts
11+
// UNSUPPORTED: gcc-10
12+
13+
// iterator, const_iterator, reverse_iterator, const_reverse_iterator
14+
15+
#include <array>
16+
17+
#include <iterator>
18+
19+
using iterator = std::array<int, 10>::iterator;
20+
using const_iterator = std::array<int, 10>::const_iterator;
21+
22+
static_assert(std::indirectly_readable<iterator>);
23+
static_assert(std::indirectly_writable<iterator, int>);
24+
25+
static_assert(std::indirectly_readable<const_iterator>);
26+
static_assert(!std::indirectly_writable<const_iterator, int>);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
// UNSUPPORTED: libcpp-no-concepts
11+
// UNSUPPORTED: gcc-10
12+
13+
// iterator, const_iterator, reverse_iterator, const_reverse_iterator
14+
15+
#include <deque>
16+
17+
#include <iterator>
18+
19+
using iterator = std::deque<int>::iterator;
20+
using const_iterator = std::deque<int>::const_iterator;
21+
using value_type = iterator::value_type;
22+
23+
static_assert(std::indirectly_readable<iterator>);
24+
static_assert(std::indirectly_writable<iterator, value_type>);
25+
26+
static_assert(std::indirectly_readable<const_iterator>);
27+
static_assert(!std::indirectly_writable<const_iterator, value_type>);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
// UNSUPPORTED: libcpp-no-concepts
11+
// UNSUPPORTED: gcc-10
12+
13+
// iterator, const_iterator
14+
15+
#include <forward_list>
16+
17+
#include <iterator>
18+
19+
using iterator = std::forward_list<int>::iterator;
20+
using const_iterator = std::forward_list<int>::const_iterator;
21+
using value_type = iterator::value_type;
22+
23+
static_assert(std::indirectly_readable<iterator>);
24+
static_assert(std::indirectly_writable<iterator, value_type>);
25+
26+
static_assert(std::indirectly_readable<const_iterator>);
27+
static_assert(!std::indirectly_writable<const_iterator, value_type>);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
// UNSUPPORTED: libcpp-no-concepts
11+
// UNSUPPORTED: gcc-10
12+
13+
// iterator, const_iterator, reverse_iterator, const_reverse_iterator
14+
15+
#include <list>
16+
17+
#include <iterator>
18+
19+
using iterator = std::list<int>::iterator;
20+
using const_iterator = std::list<int>::const_iterator;
21+
using value_type = iterator::value_type;
22+
23+
static_assert(std::indirectly_readable<iterator>);
24+
static_assert(std::indirectly_writable<iterator, value_type>);
25+
26+
static_assert(std::indirectly_readable<const_iterator>);
27+
static_assert(!std::indirectly_writable<const_iterator, value_type>);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
// UNSUPPORTED: libcpp-no-concepts
11+
// UNSUPPORTED: gcc-10
12+
13+
// iterator, const_iterator, reverse_iterator, const_reverse_iterator
14+
15+
#include <vector>
16+
17+
#include <iterator>
18+
19+
using iterator = std::vector<bool>::iterator;
20+
using const_iterator = std::vector<bool>::const_iterator;
21+
using value_type = iterator::value_type;
22+
23+
static_assert(std::indirectly_readable<iterator>);
24+
static_assert(!std::indirectly_writable<iterator, value_type>);
25+
26+
static_assert(std::indirectly_readable<const_iterator>);
27+
static_assert(!std::indirectly_writable<const_iterator, value_type>);

0 commit comments

Comments
 (0)