5.1 KiB
[range.as.rvalue]
25 Ranges library [ranges]
25.7 Range adaptors [range.adaptors]
25.7.7 As rvalue view [range.as.rvalue]
25.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 an 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) ifT models input_range andsame_as<range_rvalue_reference_t, range_reference_t> is true.
-
Otherwise, as_rvalue_view(E).
[Example 1: vector words = {"the", "quick", "brown", "fox", "ate", "a", "pterodactyl"}; vector new_words; ranges::copy(words | views::as_rvalue, back_inserter(new_words)); // moves each string from words into new_words â end example]
25.7.7.2 Class template as_rvalue_view [range.as.rvalue.view]
namespace std::ranges {template<view V>requires input_rangeclass as_rvalue_view : public view_interface<as_rvalue_view> { V base_ = V(); // exposition onlypublic: as_rvalue_view() requires default_initializable = default; constexpr explicit as_rvalue_view(V base); constexpr V base() const & requires copy_constructible { return base_; }constexpr V base() && { return std::move(base_); }constexpr auto begin() requires (){ return move_iterator(ranges::begin(base_)); }constexpr auto begin() const requires range{ return move_iterator(ranges::begin(base_)); }constexpr auto end() requires (
) {if constexpr (common_range) {return move_iterator(ranges::end(base_)); } else {return move_sentinel(ranges::end(base_)); }}constexpr auto end() const requires range {if constexpr (common_range) {return move_iterator(ranges::end(base_)); } else {return move_sentinel(ranges::end(base_)); }}constexpr auto size() requires sized_range { return ranges::size(base_); }constexpr auto size() const requires sized_range { return ranges::size(base_); }constexpr auto reserve_hint() requires approximately_sized_range{ return ranges::reserve_hint(base_); }constexpr auto reserve_hint() const requires approximately_sized_range{ return ranges::reserve_hint(base_); }}; template as_rvalue_view(R&&) -> as_rvalue_view<views::all_t>;}
constexpr explicit as_rvalue_view(V base);
Effects: Initializes base_ with std::move(base).