Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LWG-4112 has-arrow should require operator->() to be const-qualified #5152

Merged
merged 4 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stl/inc/ranges
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ namespace ranges {
&& same_as<sentinel_t<_Rng>, sentinel_t<const _Rng>>;

template <class _It>
concept _Has_arrow = input_iterator<_It> && (is_pointer_v<_It> || _Has_member_arrow<_It&>);
concept _Has_arrow = input_iterator<_It> && (is_pointer_v<_It> || _Has_member_arrow<const _It&>);

template <bool _IsWrapped, class _Ty>
using _Maybe_wrapped = conditional_t<_IsWrapped, _Ty, _Unwrapped_t<_Ty>>;
Expand Down
3 changes: 3 additions & 0 deletions tests/libcxx/expected_results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ std/ranges/range.factories/range.iota.view/iterator/member_typedefs.compile.pass
std/ranges/range.adaptors/range.lazy.split/range.lazy.split.outer.value/ctor.default.pass.cpp FAIL
std/ranges/range.adaptors/range.lazy.split/range.lazy.split.outer.value/ctor.iter.pass.cpp FAIL

# libc++ doesn't implement LWG-4112
std/ranges/range.adaptors/range.join/range.join.iterator/arrow.pass.cpp FAIL

# If any feature-test macro test is failing, this consolidated test will also fail.
std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp FAIL

Expand Down
48 changes: 48 additions & 0 deletions tests/std/tests/P0896R4_views_filter/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <forward_list>
#include <ranges>
#include <span>
Expand Down Expand Up @@ -347,6 +348,53 @@ using move_only_view = test::range<Category, const int, test::Sized{is_random},
IsCommon, test::CanCompare{derived_from<Category, forward_iterator_tag>},
test::ProxyRef{!derived_from<Category, contiguous_iterator_tag>}, test::CanView::yes, test::Copyability::move_only>;

// LWG-4112 "possibly-const-range should prefer returning const R&"

template <class T>
concept CanArrow = requires(T&& t) { forward<T>(t).operator->(); };

enum class arrow_status : bool { bad, good };

template <arrow_status S>
struct arrowed_iterator {
using value_type = int;
using difference_type = ptrdiff_t;

int& operator*() const {
return *p_;
}

int* operator->()
requires (S == arrow_status::bad)
{
return p_;
}
int* operator->() const
requires (S == arrow_status::good)
{
return p_;
}

arrowed_iterator& operator++() {
++p_;
return *this;
}
arrowed_iterator operator++(int) {
auto old = *this;
++*this;
return old;
}

friend bool operator==(arrowed_iterator, arrowed_iterator) = default;

int* p_;
};

static_assert(CanArrow<ranges::iterator_t<decltype(ranges::subrange<arrowed_iterator<arrow_status::good>>{} //
| views::filter(is_even))>>);
static_assert(!CanArrow<ranges::iterator_t<decltype(ranges::subrange<arrowed_iterator<arrow_status::bad>>{} //
| views::filter(is_even))>>);

int main() {
// Validate views
{ // ... copyable
Expand Down
55 changes: 55 additions & 0 deletions tests/std/tests/P0896R4_views_join/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <algorithm>
#include <array>
#include <cassert>
#include <cstddef>
#include <forward_list>
#include <list>
#include <ranges>
Expand Down Expand Up @@ -688,6 +689,60 @@ constexpr bool test_lwg3791() {
return true;
}

// LWG-4112 "possibly-const-range should prefer returning const R&"

template <class T>
concept CanArrow = requires(T&& t) { forward<T>(t).operator->(); };

enum class arrow_status : bool { bad, good };

template <arrow_status S>
struct arrowed_iterator {
using value_type = int;
using difference_type = ptrdiff_t;

int& operator*() const {
return *p_;
}

int* operator->()
requires (S == arrow_status::bad)
{
return p_;
}
int* operator->() const
requires (S == arrow_status::good)
{
return p_;
}

arrowed_iterator& operator++() {
++p_;
return *this;
}
arrowed_iterator operator++(int) {
auto old = *this;
++*this;
return old;
}

friend bool operator==(arrowed_iterator, arrowed_iterator) = default;

int* p_;
};

void test_lwg_4112() { // COMPILE-ONLY
using good_inner_range = ranges::subrange<arrowed_iterator<arrow_status::good>>;
using good_nested_range = span<const good_inner_range>;
using good_joined_range = decltype(good_nested_range{} | views::join);
static_assert(CanArrow<ranges::iterator_t<good_joined_range>>);

using bad_inner_range = ranges::subrange<arrowed_iterator<arrow_status::bad>>;
using bad_nested_range = span<const bad_inner_range>;
using bad_joined_range = decltype(bad_nested_range{} | views::join);
static_assert(!CanArrow<ranges::iterator_t<bad_joined_range>>);
}

int main() {
// Validate views
constexpr string_view expected = "Hello World!"sv;
Expand Down