Skip to content

[libc++][ranges] LWG3715: view_interface::empty is overconstrained #85004

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

Merged
merged 6 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 libcxx/docs/Status/Cxx23Issues.csv
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
"`3711 <https://wg21.link/LWG3711>`__","Missing preconditions for slide_view constructor","July 2022","","","|ranges|"
"`3712 <https://wg21.link/LWG3712>`__","``chunk_view`` and ``slide_view`` should not be ``default_initializable``","July 2022","","","|ranges|"
"`3713 <https://wg21.link/LWG3713>`__","Sorted with respect to comparator (only)","July 2022","",""
"`3715 <https://wg21.link/LWG3715>`__","``view_interface::empty`` is overconstrained","July 2022","","","|ranges|"
"`3715 <https://wg21.link/LWG3715>`__","``view_interface::empty`` is overconstrained","July 2022","|Complete|","19.0","|ranges|"
"`3719 <https://wg21.link/LWG3719>`__","Directory iterators should be usable with default sentinel","July 2022","|Complete|","17.0","|ranges|"
"`3721 <https://wg21.link/LWG3721>`__","Allow an ``arg-id`` with a value of zero for ``width`` in ``std-format-spec``","July 2022","|Complete|","16.0","|format|"
"`3724 <https://wg21.link/LWG3724>`__","``decay-copy`` should be constrained","July 2022","|Complete|","14.0"
Expand Down
17 changes: 13 additions & 4 deletions libcxx/include/__ranges/view_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <__ranges/access.h>
#include <__ranges/concepts.h>
#include <__ranges/empty.h>
#include <__ranges/size.h>
#include <__type_traits/is_class.h>
#include <__type_traits/make_unsigned.h>
#include <__type_traits/remove_cv.h>
Expand Down Expand Up @@ -51,16 +52,24 @@ class view_interface {
public:
template <class _D2 = _Derived>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty()
Comment on lines 53 to 54
Copy link
Member Author

@xiaoyang-sde xiaoyang-sde Mar 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although unrelated to this pull request, I'm curious about the reason behind this method being a member template. It appears that this differs from the implementation in libstdc++.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be related to a Clang bug when we implemented this early on. I vaguely remember that there were bugs related to constraints on member functions in Clang a while back, and I think we had to work around it in a few places. But it could also be my memory playing tricks on me, so it's worth double-checking that information.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be related to a Clang bug when we implemented this early on. I vaguely remember that there were bugs related to constraints on member functions in Clang a while back, and I think we had to work around it in a few places. But it could also be my memory playing tricks on me, so it's worth double-checking that information.

Ah I see. Thanks for the explanation.

requires forward_range<_D2>
requires sized_range<_D2> || forward_range<_D2>
{
return ranges::begin(__derived()) == ranges::end(__derived());
if constexpr (sized_range<_D2>) {
return ranges::size(__derived()) == 0;
} else {
return ranges::begin(__derived()) == ranges::end(__derived());
}
}

template <class _D2 = _Derived>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const
requires forward_range<const _D2>
requires sized_range<const _D2> || forward_range<const _D2>
{
return ranges::begin(__derived()) == ranges::end(__derived());
if constexpr (sized_range<const _D2>) {
return ranges::size(__derived()) == 0;
} else {
return ranges::begin(__derived()) == ranges::end(__derived());
}
}

template <class _D2 = _Derived>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ struct InputRange : std::ranges::view_interface<InputRange> {
constexpr InputIter end() const { return InputIter(buff + 8); }
};

struct SizedInputRange : std::ranges::view_interface<SizedInputRange> {
int buff[8] = {0, 1, 2, 3, 4, 5, 6, 7};
constexpr InputIter begin() const { return InputIter(buff); }
constexpr sentinel_wrapper<InputIter> end() const { return sentinel_wrapper(InputIter(buff + 8)); }
constexpr std::size_t size() const { return 8; }
};
static_assert(std::ranges::sized_range<SizedInputRange>);

struct NotSizedSentinel {
using value_type = int;
using difference_type = std::ptrdiff_t;
Expand Down Expand Up @@ -155,11 +163,24 @@ concept BoolOpInvocable = requires (T const& obj) { bool(obj); };

constexpr bool testEmpty() {
static_assert(!EmptyInvocable<InputRange>);
// LWG 3715: `view_interface::empty` is overconstrained
static_assert(EmptyInvocable<SizedInputRange>);
static_assert( EmptyInvocable<ForwardRange>);

static_assert(!BoolOpInvocable<InputRange>);
static_assert(BoolOpInvocable<SizedInputRange>);
static_assert( BoolOpInvocable<ForwardRange>);

SizedInputRange sizedInputRange;
assert(!sizedInputRange.empty());
assert(!static_cast<SizedInputRange const&>(sizedInputRange).empty());

assert(sizedInputRange);
assert(static_cast<SizedInputRange const&>(sizedInputRange));

assert(!std::ranges::empty(sizedInputRange));
assert(!std::ranges::empty(static_cast<SizedInputRange const&>(sizedInputRange)));

ForwardRange forwardRange;
assert(!forwardRange.empty());
assert(!static_cast<ForwardRange const&>(forwardRange).empty());
Expand Down