std::ranges::views::as_rvalue, std::ranges::as_rvalue_view
From cppreference.com
| Defined in header <ranges>
|
||
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.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,Tmodelsinput_range, andstd::same_as<ranges::range_rvalue_reference_t<T>, ranges::range_reference_t<T>>istrue;as_rvalue_view{e}otherwise.
Data members
| Member | Description |
V base_ (private)
|
the underlying view (exposition-only member object*) |
Member functions
constructs an as_rvalue_view (public member function) | |
returns the underlying view V (public member function) | |
returns the beginning iterator of the as_rvalue_view (public member function) | |
returns the end iterator of the as_rvalue_view (public member function) | |
| returns the size of the view if it is bounded (public member function) | |
(C++26) |
returns the approximate size of the underlying approximately_sized_range (public member function) |
Inherited from ranges::view_interface | |
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>)
| |
(C++23) |
returns a constant iterator to the beginning of the range (public member function of std::ranges::view_interface<D>)
|
(C++23) |
returns a sentinel for the constant iterator of the range (public member function of std::ranges::view_interface<D>)
|
| 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>)
| |
returns the first element in the derived view, provided if it satisfies forward_range (public member function of std::ranges::view_interface<D>)
| |
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>)
| |
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>)
| |
std::ranges::as_rvalue_view::as_rvalue_view
as_rvalue_view() requires std::default_initializable<V> = default; |
(1) | (since C++23) |
constexpr explicit as_rvalue_view( V base ); |
(2) | (since C++23) |
Parameters
| base | - | a view |
std::ranges::as_rvalue_view::base
constexpr V base() const& requires std::copy_constructible<V>; |
(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_ ;.2) Move-constructs the result from the underlying view. Equivalent to
return std::move(base_ );.
std::ranges::as_rvalue_view::begin
constexpr auto begin() requires (!/*simple-view*/<V>); |
(1) | (since C++23) |
constexpr auto begin() const requires ranges::range<const V>; |
(2) | (since C++23) |
Returns std::move_iterator(ranges::begin(base_ )).
std::ranges::as_rvalue_view::end
constexpr auto end() requires (!/*simple-view*/<V>); |
(1) | (since C++23) |
constexpr auto end() const requires ranges::range<const V>; |
(2) | (since C++23) |
Returns std::move_iterator(ranges::end(base_ )) if (1) V or (2) const V models common_range.
Returns std::move_sentinel(ranges::end(base_ )) otherwise.
std::ranges::as_rvalue_view::size
constexpr auto size() requires ranges::sized_range<V>; |
(1) | (since C++23) |
constexpr auto size() const requires ranges::sized_range<const V>; |
(2) | (since C++23) |
Returns the size of the view if the view is bounded. Equivalent to return ranges::size(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_ ).
Deduction guides
template< class R > as_rvalue_view( R&& ) -> as_rvalue_view<views::all_t<R>>; |
(since C++23) | |
Helper templates
template< class T > constexpr bool enable_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 | 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
|
Example
Run this code
#include <algorithm>
#include <iostream>
#include <ranges>
#include <string>
#include <vector>
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”
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
(C++20) |
casts the result of dereferencing an object to its associated rvalue reference type (customization point object) |
(C++11) |
iterator adaptor which dereferences to an rvalue (class template) |
(C++20) |
sentinel adaptor for std::move_iterator (class template) |
converts a view into a constant_range(class template) (range adaptor object) |