std::ranges::views::as_rvalue, std::ranges::as_rvalue_view - cppreference.com (original) (raw)

Defined in header
template< ranges::view V > requires ranges::input_range<V> class as_rvalue_view : public ranges::view_interface<as_rvalue_view<V>> (1) (since C++23)
namespace views { inline constexpr /* unspecified */ as_rvalue = /* unspecified */; } (2) (since C++23)
Call signature
template< ranges::viewable_range R > requires /* see below */ constexpr ranges::view auto as_rvalue( R&& r ); (since C++23)
  1. A range adaptor that represents a view of underlying view whose elements are rvalues.

Contents

[edit] Data members

Member Description
V base_ (private) the underlying view(exposition-only member object*)

[edit] Member functions

(constructor) constructs an as_rvalue_view (public member function)
base returns the underlying view V (public member function)
begin returns the beginning iterator of the as_rvalue_view (public member function)
end returns the end iterator of the as_rvalue_view (public member function)
size returns the size of the view if it is bounded (public member function)
reserve_hint(C++26) returns the approximate size of the underlying approximately_sized_range (public member function)
Inherited from std::ranges::view_interface
empty returns whether the derived view is empty, provided only if it satisfies sized_range or forward_range (public member function of std::ranges::view_interface) [edit]
cbegin(C++23) returns a constant iterator to the beginning of the range (public member function of std::ranges::view_interface) [edit]
cend(C++23) returns a sentinel for the constant iterator of the range (public member function of std::ranges::view_interface) [edit]
operator bool returns whether the derived view is not empty, provided only if ranges::empty is applicable to it (public member function of std::ranges::view_interface) [edit]
front returns the first element in the derived view, provided if it satisfies forward_range (public member function of std::ranges::view_interface) [edit]
back returns the last element in the derived view, provided only if it satisfies bidirectional_range and common_range (public member function of std::ranges::view_interface) [edit]
operator[] returns the nth element in the derived view, provided only if it satisfies random_access_range (public member function of std::ranges::view_interface) [edit]

std::ranges::as_rvalue_view::as_rvalue_view

| | (1) | (since C++23) | | | ---------------------------------------------- | ------------- | ------------- | | constexpr explicit as_rvalue_view( V base ); | (2) | (since C++23) |

  1. Value-initializes _[base](as%5Frvalue%5Fview.html#base)_ via its default member initializer (= V()).

  2. Initializes _[base](as%5Frvalue%5Fview.html#base)_ with std::move(base).

Parameters

std::ranges::as_rvalue_view::base

| | (1) | (since C++23) | | | ---------------------- | ------------- | ------------- | | constexpr V base() &&; | (2) | (since C++23) |

Returns the underlying view.

  1. Copy-constructs the result from the underlying view. Equivalent to return _[base](as%5Frvalue%5Fview.html#base)_ ;.

  2. Move-constructs the result from the underlying view. Equivalent to return std::move(_[base](as%5Frvalue%5Fview.html#base)_ );.

std::ranges::as_rvalue_view::size

Returns the size of the view if the view is bounded. Equivalent to return ranges::size(_[base](as%5Frvalue%5Fview.html#base)_ );.

std::ranges::as_rvalue_view::reserve_hint

constexpr auto reserve_hint() requires ranges::approximately_sized_range<V>; (1) (since C++26)
constexpr auto reserve_hint() const requires ranges::approximately_sized_range<const V>; (2) (since C++26)

Returns ranges::reserve_hint(_[base](as%5Frvalue%5Fview.html#base)_ ).

[edit] Deduction guides

| template< class R >as_rvalue_view( R&& ) -> as_rvalue_view<views::all_t<R>>; | | (since C++23) | | -------------------------------------------------------------------------------------------------------- | | ------------- |

[edit] Helper templates

This specialization of ranges::enable_borrowed_range makes as_rvalue_view satisfy borrowed_range when the underlying view satisfies it.

[edit] Notes

Feature-test macro Value Std Feature
__cpp_lib_ranges_as_rvalue 202207L (C++23) std::ranges::as_rvalue_view
__cpp_lib_ranges_reserve_hint 202502L (C++26) ranges::approximately_sized_range and reserve_hint

[edit] Example

#include #include #include #include #include   int main() { std::vector<std::string> words = {"Quick", "red", "\N{FOX FACE}", "jumped", "over", "a", "pterodactyl"}; std::vector<std::string> new_words;   std::ranges::copy( words | std::views::as_rvalue, std::back_inserter(new_words)); // move string from words into new_words   auto quoted = std::views::transform([](auto&& s) { return "“" + s + "”"; });   std::cout << "Old words: "; for (auto&& word : words | std::views::as_rvalue | quoted) std::cout << word << ' ';   std::cout << "\nNew words: "; for (auto&& word : new_words | std::views::as_rvalue | quoted) std::cout << word << ' '; }

Possible output:

Old words: “” “” “” “” “” “” “” New words: “Quick” “red” “🦊” “jumped” “over” “a” “pterodactyl”

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 4083 C++23 views::as_rvalue used to accept non-input ranges made rejected

[edit] See also