26 Ranges library [ranges]

26.7 Range adaptors [range.adaptors]

26.7.7 As rvalue view [range.as.rvalue]

26.7.7.1 Overview [range.as.rvalue.overview]

as_­rvalue_­view presents a view of an underlying sequence with the same behavior as the underlying sequence except that its elements are rvalues.
Some generic algorithms can be called with a as_­rvalue_­view to replace copying with moving.
The name views​::​as_­rvalue denotes a range adaptor object ([range.adaptor.object]).
Let E be an expression and let T be decltype((E)).
The expression views​::​as_­rvalue(E) is expression-equivalent to:
  • views​::​all(E) if same_­as<range_­rvalue_­reference_­t<T>, range_­reference_­t<T>> is true.
  • Otherwise, as_­rvalue_­view(E).
[Example 1: vector<string> words = {"the", "quick", "brown", "fox", "ate", "a", "pterodactyl"}; vector<string> new_words; ranges::copy(words | views::as_rvalue, back_inserter(new_words)); // moves each string from words into new_­words — end example]

26.7.7.2 Class template as_­rvalue_­view [range.as.rvalue.view]

namespace std::ranges { template<view V> requires input_­range<V> class as_rvalue_view : public view_interface<as_rvalue_view<V>> { V base_ = V(); // exposition only public: as_rvalue_view() requires default_­initializable<V> = default; constexpr explicit as_rvalue_view(V base); constexpr V base() const & requires copy_­constructible<V> { return base_; } constexpr V base() && { return std::move(base_); } constexpr auto begin() requires (!simple-view<V>) { return move_iterator(ranges::begin(base_)); } constexpr auto begin() const requires range<const V> { return move_iterator(ranges::begin(base_)); } constexpr auto end() requires (!simple-view<V>) { if constexpr (common_­range<V>) { return move_iterator(ranges::end(base_)); } else { return move_sentinel(ranges::end(base_)); } } constexpr auto end() const requires range<const V> { if constexpr (common_­range<const V>) { return move_iterator(ranges::end(base_)); } else { return move_sentinel(ranges::end(base_)); } } constexpr auto size() requires sized_­range<V> { return ranges::size(base_); } constexpr auto size() const requires sized_­range<const V> { return ranges::size(base_); } }; template<class R> as_rvalue_view(R&&) -> as_rvalue_view<views::all_t<R>>; }
constexpr explicit as_rvalue_view(V base);
Effects: Initializes base_­ with std​::​move(base).