Skip to content

Commit

Permalink
Implement LWG-4083 views::as_rvalue should reject non-input ranges (#…
Browse files Browse the repository at this point in the history
…4786)

Co-authored-by: Stephan T. Lavavej <[email protected]>
  • Loading branch information
frederick-vs-ja and StephanTLavavej authored Jul 11, 2024
1 parent 39dd503 commit a395844
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
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>);

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

0 comments on commit a395844

Please sign in to comment.