std::ranges::views::as_rvalue, std::ranges::as_rvalue_view - cppreference.com

From cppreference.com

Defined in header

<ranges>

template<ranges::viewV>requiresranges::input_range<V>classas_rvalue_view:publicranges::view_interface<as_rvalue_view<V>> (1) (since C++23)namespaceviews{inlineconstexpr/* unspecified */as_rvalue=/* unspecified */;} (2) (since C++23)Call signature

template<ranges::viewable_rangeR>requires/* see below */constexprranges::viewautoas_rvalue(R&&r);(since C++23)1) A range adaptor that represents a view of underlying

view

whose elements are rvalues.

2)

RangeAdaptorObject

. Let e be a subexpression and let T be decltype((e)). Then the expression views::as_rvalue(e) is

expression-equivalent

to:

views::all(e), if it is a well-formed expression, T models

input_range

, and std::same_as<ranges::range_rvalue_reference_t<T>,ranges::range_reference_t<T>> is true;

as_rvalue_view{e} otherwise.

Data members

Member Description Vbase_(private)the underlying view
(exposition-only member object*)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<D>)

[edit]

cbegin

(C++23)

returns a constant iterator to the beginning of the range
(public member function of std::ranges::view_interface<D>)

[edit]

cend

(C++23)

returns a sentinel for the constant iterator of the range
(public member function of std::ranges::view_interface<D>)

[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<D>)

[edit]

front

returns the first element in the derived view, provided if it satisfies

forward_range

(public member function of std::ranges::view_interface<D>)

[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<D>)

[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<D>)

[edit]

std::ranges::as_rvalue_view::as_rvalue_view

as_rvalue_view()requiresstd::default_initializable<V>=default; (1) (since C++23)constexprexplicitas_rvalue_view(Vbase); (2) (since C++23)1) Value-initializes

base_

via its default member initializer (=V()).

2) Initializes

base_

with std::move(base).

Parameters

base - a view std::ranges::as_rvalue_view::base

constexprVbase()const&requiresstd::copy_constructible<V>; (1) (since C++23)constexprVbase()&&; (2) (since C++23)Returns the underlying view.

1) Copy-constructs the result from the underlying view. Equivalent to return

base_

;.

2) Move-constructs the result from the underlying view. Equivalent to returnstd::move(

base_

);.

std::ranges::as_rvalue_view::begin

constexprautobegin()requires(!/*simple-view*/<V>); (1) (since C++23)constexprautobegin()constrequiresranges::range<constV>; (2) (since C++23)Returns std::move_iterator(ranges::begin(

base_

)).

std::ranges::as_rvalue_view::end

constexprautoend()requires(!/*simple-view*/<V>); (1) (since C++23)constexprautoend()constrequiresranges::range<constV>; (2) (since C++23)Returns std::move_iterator(ranges::end(

base_

)) if (1)V or (2)constV models

common_range

.

Returns std::move_sentinel(ranges::end(

base_

)) otherwise.

std::ranges::as_rvalue_view::size

constexprautosize()requiresranges::sized_range<V>; (1) (since C++23)constexprautosize()constrequiresranges::sized_range<constV>; (2) (since C++23)Returns the size of the view if the view is bounded. Equivalent to returnranges::size(

base_

);.

std::ranges::as_rvalue_view::reserve_hint

constexprautoreserve_hint()requiresranges::approximately_sized_range<V>; (1) (since C++26)constexprautoreserve_hint()constrequiresranges::approximately_sized_range<constV>; (2) (since C++26)Returns ranges::reserve_hint(

base_

).

Deduction guides

template<classR>as_rvalue_view(R&&)->as_rvalue_view<views::all_t<R>>;(since C++23)Helper templates

template<classT>constexprboolenable_borrowed_range<std::ranges::as_rvalue_view<T>>=ranges::enable_borrowed_range<T>;(since C++23)This specialization of

ranges::enable_borrowed_range

makes as_rvalue_view satisfy

borrowed_range

when the underlying view satisfies it.

Notes

Feature-test

macro ValueStdFeature

__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

Example

Run this code

#include<algorithm>#include<iostream>#include<ranges>#include<string>#include<vector>intmain(){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_wordsautoquoted=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” 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 See also