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-4083 views::as_rvalue should reject non-input ranges #4786

Merged
merged 5 commits into from
Jul 11, 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
10 changes: 6 additions & 4 deletions stl/inc/ranges
Original file line number Diff line number Diff line change
Expand Up @@ -1523,19 +1523,21 @@ namespace ranges {
constexpr auto _Compile_time_max_size<const as_rvalue_view<_Rng>> = _Compile_time_max_size<const _Rng>;

namespace views {
// direct-non-list-initialization is specified in the Standard (N4981 [range.as.rvalue.overview]/2.2)
// and needed for EDG, DevCom-10698021
template <class _Rng>
concept _Can_as_rvalue = requires(_Rng&& __r) { as_rvalue_view{static_cast<_Rng&&>(__r)}; };
concept _Can_as_rvalue = requires(_Rng&& __r) { as_rvalue_view(static_cast<_Rng&&>(__r)); };

class _As_rvalue_fn : public _Pipe::_Base<_As_rvalue_fn> {
private:
enum class _St { _None, _All, _As_rvalue };

template <class _Rng>
_NODISCARD static consteval _Choice_t<_St> _Choose() noexcept {
if constexpr (same_as<range_rvalue_reference_t<_Rng>, range_reference_t<_Rng>>) {
if constexpr (input_range<_Rng> && same_as<range_rvalue_reference_t<_Rng>, range_reference_t<_Rng>>) {
return {_St::_All, noexcept(views::all(_STD declval<_Rng>()))};
} else if constexpr (_Can_as_rvalue<_Rng>) {
return {_St::_As_rvalue, noexcept(as_rvalue_view{_STD declval<_Rng>()})};
return {_St::_As_rvalue, noexcept(as_rvalue_view(_STD declval<_Rng>()))};
} else {
return {_St::_None};
}
Expand All @@ -1553,7 +1555,7 @@ namespace ranges {
if constexpr (_Strat == _St::_All) {
return views::all(_STD forward<_Rng>(_Range));
} else if constexpr (_Strat == _St::_As_rvalue) {
return as_rvalue_view{_STD forward<_Rng>(_Range)};
return as_rvalue_view(_STD forward<_Rng>(_Range));
} else {
_STL_INTERNAL_STATIC_ASSERT(false); // unexpected strategy
}
Expand Down
11 changes: 11 additions & 0 deletions tests/std/tests/P2446R2_views_as_rvalue/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,17 @@ void test_example_from_p2446r2() {
assert(ranges::all_of(words, ranges::empty)); // all strings from words are empty (implementation assumption)
}

// LWG-4083 "views::as_rvalue should reject non-input ranges"
struct OutputRvalueIterator {
using difference_type = int;
int operator*() const;
OutputRvalueIterator& operator++();
void operator++(int);
};
using OutputRvalueRange = decltype(ranges::subrange{OutputRvalueIterator{}, unreachable_sentinel});

static_assert(!CanViewAsRvalue<OutputRvalueRange>);
frederick-vs-ja marked this conversation as resolved.
Show resolved Hide resolved

int main() {
{ // Validate views
// ... copyable
Expand Down