From ef4a46551f50afe01388659e41dbdb421a17ef63 Mon Sep 17 00:00:00 2001 From: "S. B. Tam" Date: Sat, 29 May 2021 08:56:27 +0800 Subject: [PATCH 1/8] Repairing `counted_iterator` --- stl/inc/iterator | 54 +++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/stl/inc/iterator b/stl/inc/iterator index 195a8dd61b0..d24711c1b2e 100644 --- a/stl/inc/iterator +++ b/stl/inc/iterator @@ -1051,10 +1051,35 @@ struct iterator_traits> { }; // CLASS TEMPLATE counted_iterator +template +struct _Counted_iterator_value_type_base {}; + +template +struct _Counted_iterator_value_type_base<_Iter> { + using value_type = iter_value_t<_Iter>; +}; + +template +struct _Counted_iterator_category_base : _Counted_iterator_value_type_base<_Iter> {}; + +template <_Has_member_iterator_category _Iter> +struct _Counted_iterator_category_base<_Iter> : _Counted_iterator_value_type_base<_Iter> { + using iterator_category = typename _Iter::iterator_category; +}; + +template +struct _Counted_iterator_concept_base : _Counted_iterator_category_base<_Iter> {}; + +template <_Has_member_iterator_concept _Iter> +struct _Counted_iterator_concept_base<_Iter> : _Counted_iterator_category_base<_Iter> { + using iterator_concept = typename _Iter::iterator_concept; +}; + template -class counted_iterator { +class counted_iterator : public _Counted_iterator_concept_base<_Iter> { public: - using iterator_type = _Iter; + using iterator_type = _Iter; + using difference_type = iter_difference_t<_Iter>; // [counted.iter.const] constexpr counted_iterator() = default; @@ -1112,6 +1137,10 @@ public: return *_Current; } + _NODISCARD constexpr auto operator->() const noexcept requires contiguous_iterator<_Iter> { + return _STD to_address(_Current); + } + _NODISCARD constexpr decltype(auto) operator[](const iter_difference_t<_Iter> _Diff) const requires random_access_iterator<_Iter> { #if _ITERATOR_DEBUG_LEVEL != 0 @@ -1336,25 +1365,12 @@ private: iter_difference_t<_Iter> _Length = 0; }; -template -struct incrementable_traits> { - using difference_type = iter_difference_t<_Iter>; -}; - +// clang-format off template + requires (!_Is_from_primary>) struct iterator_traits> : iterator_traits<_Iter> { - using pointer = void; -}; - -template -struct pointer_traits> { // TRANSITION, address LWG-3408 and include this - using pointer = counted_iterator<_Iter>; - using element_type = remove_reference_t>; - using difference_type = iter_difference_t<_Iter>; - - _NODISCARD static constexpr element_type* to_address(const pointer _It) noexcept { - return _STD to_address(_It.base()); - } + // clang-format on + using pointer = conditional_t, add_pointer_t>, void>; }; #endif // __cpp_lib_concepts From 7ffe03894f3551e005ed9fdf4f88207e9816f59f Mon Sep 17 00:00:00 2001 From: "S. B. Tam" Date: Sat, 29 May 2021 09:50:13 +0800 Subject: [PATCH 2/8] Fix test --- tests/std/tests/P0896R4_common_iterator/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/std/tests/P0896R4_common_iterator/test.cpp b/tests/std/tests/P0896R4_common_iterator/test.cpp index 317118c1144..6c1797b7bd9 100644 --- a/tests/std/tests/P0896R4_common_iterator/test.cpp +++ b/tests/std/tests/P0896R4_common_iterator/test.cpp @@ -189,7 +189,7 @@ bool test_operator_arrow() { assert(*countedIter == P(0, 1)); assert(countedIter->first == 0); assert(countedIter->second == 1); - static_assert(is_same_v()), P*>); + static_assert(is_same_v()), P* const&>); return true; } From 420b60451ab8adb17409292268d01bf8d80849de Mon Sep 17 00:00:00 2001 From: "S. B. Tam" Date: Sat, 29 May 2021 10:20:40 +0800 Subject: [PATCH 3/8] Really fix test --- tests/std/tests/P0896R4_common_iterator/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/std/tests/P0896R4_common_iterator/test.cpp b/tests/std/tests/P0896R4_common_iterator/test.cpp index 6c1797b7bd9..1c7b93b40ad 100644 --- a/tests/std/tests/P0896R4_common_iterator/test.cpp +++ b/tests/std/tests/P0896R4_common_iterator/test.cpp @@ -189,7 +189,7 @@ bool test_operator_arrow() { assert(*countedIter == P(0, 1)); assert(countedIter->first == 0); assert(countedIter->second == 1); - static_assert(is_same_v()), P* const&>); + static_assert(is_same_v()), counted_iterator const&>); return true; } From 7f0081a0388067b0472ebce0d45cdbe1bd8e0fc0 Mon Sep 17 00:00:00 2001 From: "S. B. Tam" Date: Sun, 20 Jun 2021 21:29:45 +0800 Subject: [PATCH 4/8] Add test cases --- .../tests/P0896R4_counted_iterator/test.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/std/tests/P0896R4_counted_iterator/test.cpp b/tests/std/tests/P0896R4_counted_iterator/test.cpp index 0cec96ad8db..bf808bd5bbe 100644 --- a/tests/std/tests/P0896R4_counted_iterator/test.cpp +++ b/tests/std/tests/P0896R4_counted_iterator/test.cpp @@ -302,6 +302,40 @@ struct instantiator { } }; +// Also test P2259R1 Repairing input range adaptors and counted_iterator +struct simple_input_iter { + using value_type = double; + using difference_type = long; + using iterator_category = input_iterator_tag; + using iterator_concept = forward_iterator_tag; + + value_type operator*() const; + simple_input_iter& operator++(); + simple_input_iter operator++(int); + + bool operator==(simple_input_iter const&) const; +}; + +using CI = counted_iterator; + +static_assert(same_as::iterator_category, input_iterator_tag>); +static_assert(forward_iterator); +static_assert(forward_iterator); +static_assert(!contiguous_iterator); +static_assert(same_as); +static_assert(same_as); +static_assert(same_as); + +void test_P2259() { + struct A { int m; }; + A a[2] = { 1, 2 }; + counted_iterator ci{a, 2}; + reverse_iterator ri{ci + 1}; + static_assert(contiguous_iterator); + assert(ci->m == 1); + assert(ri->m == 1); +} + int main() { STATIC_ASSERT((with_writable_iterators::call(), true)); with_writable_iterators::call(); @@ -314,4 +348,6 @@ int main() { _Seek_wrapped(ci, uci); assert((ci == counted_iterator{ranges::next(lst.begin()), 1})); } + + test_P2259(); } From a91078e8895d9ee08ea975281ad8a6bede3ab014 Mon Sep 17 00:00:00 2001 From: "S. B. Tam" Date: Tue, 22 Jun 2021 21:15:49 +0800 Subject: [PATCH 5/8] Update tests/std/tests/P0896R4_counted_iterator/test.cpp Co-authored-by: Casey Carter --- tests/std/tests/P0896R4_counted_iterator/test.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/std/tests/P0896R4_counted_iterator/test.cpp b/tests/std/tests/P0896R4_counted_iterator/test.cpp index bf808bd5bbe..8908f960a44 100644 --- a/tests/std/tests/P0896R4_counted_iterator/test.cpp +++ b/tests/std/tests/P0896R4_counted_iterator/test.cpp @@ -327,8 +327,10 @@ static_assert(same_as); static_assert(same_as); void test_P2259() { - struct A { int m; }; - A a[2] = { 1, 2 }; + struct A { + int m; + }; + A a[2] = {1, 2}; counted_iterator ci{a, 2}; reverse_iterator ri{ci + 1}; static_assert(contiguous_iterator); From 67006def9219b67bb5c2962488f681a2bc92a53c Mon Sep 17 00:00:00 2001 From: cpplearner Date: Tue, 22 Jun 2021 21:31:59 +0800 Subject: [PATCH 6/8] Address Casey's comments --- stl/inc/yvals_core.h | 2 ++ tests/std/tests/P0896R4_counted_iterator/test.cpp | 14 +++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/stl/inc/yvals_core.h b/stl/inc/yvals_core.h index 3b2c62061be..055e60f1e9d 100644 --- a/stl/inc/yvals_core.h +++ b/stl/inc/yvals_core.h @@ -245,6 +245,8 @@ // P2102R0 Making "Implicit Expression Variations" More Explicit // P2106R0 Range Algorithm Result Types // P2116R0 Removing tuple-Like Protocol Support From Fixed-Extent span +// P2259R1 Repairing Input Range Adaptors And counted_iterator +// (partially implemented) // P????R? directory_entry::clear_cache() // _HAS_CXX20 indirectly controls: diff --git a/tests/std/tests/P0896R4_counted_iterator/test.cpp b/tests/std/tests/P0896R4_counted_iterator/test.cpp index 8908f960a44..35b80a2f66a 100644 --- a/tests/std/tests/P0896R4_counted_iterator/test.cpp +++ b/tests/std/tests/P0896R4_counted_iterator/test.cpp @@ -303,23 +303,23 @@ struct instantiator { }; // Also test P2259R1 Repairing input range adaptors and counted_iterator -struct simple_input_iter { +struct simple_forward_iter { using value_type = double; using difference_type = long; using iterator_category = input_iterator_tag; using iterator_concept = forward_iterator_tag; value_type operator*() const; - simple_input_iter& operator++(); - simple_input_iter operator++(int); + simple_forward_iter& operator++(); + simple_forward_iter operator++(int); - bool operator==(simple_input_iter const&) const; + bool operator==(simple_forward_iter const&) const; }; -using CI = counted_iterator; +using CI = counted_iterator; -static_assert(same_as::iterator_category, input_iterator_tag>); -static_assert(forward_iterator); +static_assert(same_as::iterator_category, input_iterator_tag>); +static_assert(forward_iterator); static_assert(forward_iterator); static_assert(!contiguous_iterator); static_assert(same_as); From cc390f1873db331dd4bc7a302bdffbd78316c656 Mon Sep 17 00:00:00 2001 From: "S. B. Tam" Date: Tue, 22 Jun 2021 22:09:37 +0800 Subject: [PATCH 7/8] Update test.cpp --- tests/std/tests/P0896R4_counted_iterator/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/std/tests/P0896R4_counted_iterator/test.cpp b/tests/std/tests/P0896R4_counted_iterator/test.cpp index 35b80a2f66a..b8c0b78d16b 100644 --- a/tests/std/tests/P0896R4_counted_iterator/test.cpp +++ b/tests/std/tests/P0896R4_counted_iterator/test.cpp @@ -330,7 +330,7 @@ void test_P2259() { struct A { int m; }; - A a[2] = {1, 2}; + A a[2] = {{1}, {2}}; counted_iterator ci{a, 2}; reverse_iterator ri{ci + 1}; static_assert(contiguous_iterator); From d4543134e9c56bd55eb950194f3d89b09e3280b6 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Tue, 22 Jun 2021 18:36:47 -0700 Subject: [PATCH 8/8] Code review feedback. --- tests/std/tests/P0896R4_counted_iterator/test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/std/tests/P0896R4_counted_iterator/test.cpp b/tests/std/tests/P0896R4_counted_iterator/test.cpp index b8c0b78d16b..8da0d9abae2 100644 --- a/tests/std/tests/P0896R4_counted_iterator/test.cpp +++ b/tests/std/tests/P0896R4_counted_iterator/test.cpp @@ -313,7 +313,7 @@ struct simple_forward_iter { simple_forward_iter& operator++(); simple_forward_iter operator++(int); - bool operator==(simple_forward_iter const&) const; + bool operator==(const simple_forward_iter&) const; }; using CI = counted_iterator; @@ -323,6 +323,7 @@ static_assert(forward_iterator); static_assert(forward_iterator); static_assert(!contiguous_iterator); static_assert(same_as); +static_assert(same_as); static_assert(same_as); static_assert(same_as);