Difference between revisions of "cpp/ranges/ref view"
(P2325R3) |
m (rm members' rev-tags.) |
||
(11 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/ranges/title | ref_view}} | + | {{cpp/ranges/title|ref_view}} |
{{cpp/ranges/navbar}} | {{cpp/ranges/navbar}} | ||
− | {{ | + | {{ddcl|header=ranges|since=c++20|1= |
− | + | template< ranges::range R > | |
− | + | ||
− | template<ranges::range R> | + | |
requires std::is_object_v<R> | requires std::is_object_v<R> | ||
− | class ref_view : public ranges::view_interface<ref_view<R>> | + | class ref_view |
+ | : public ranges::view_interface<ref_view<R>> | ||
}} | }} | ||
− | |||
{{tt|ref_view}} is a {{lconcept|view}} of the elements of some other {{lconcept|range}}. It wraps a reference to that {{tt|range}}. | {{tt|ref_view}} is a {{lconcept|view}} of the elements of some other {{lconcept|range}}. It wraps a reference to that {{tt|range}}. | ||
− | {{ | + | ===Data members=== |
+ | {{dsc begin}} | ||
+ | {{dsc hitem|Member name|Definition}} | ||
+ | {{dsc expos mem obj|r_|private=yes|A pointer of type {{tt|R*}} to the underlying range.}} | ||
+ | {{dsc end}} | ||
===Member functions=== | ===Member functions=== | ||
− | {{member | {{small|std::ranges::ref_view::}}ref_view | 2= | + | {{dsc begin}} |
− | {{ddcl | since=c++20 | | + | {{dsc mem ctor|{{PAGENAME}}#ctor|constructs a {{tt|ref_view}} that references to the given range}} |
− | template< | + | {{dsc mem fun|title=base|{{PAGENAME}}#base|returns the references to the referenced range}} |
+ | {{dsc mem fun|title=begin|{{PAGENAME}}#begin|returns the beginning iterator of the referenced range}} | ||
+ | {{dsc mem fun|title=end|{{PAGENAME}}#end|returns the sentinel of the referenced range}} | ||
+ | {{dsc mem fun|title=empty|{{PAGENAME}}#empty|checks whether the referenced range is empty}} | ||
+ | {{dsc mem fun|title=size|{{PAGENAME}}#size|returns the size of the referenced {{lconcept|sized_range}}}} | ||
+ | {{dsc mem fun|title=data|{{PAGENAME}}#data|returns the pointer to the beginning of the referenced {{lconcept|contiguous_range}}}} | ||
+ | |||
+ | {{cpp/ranges/view_interface/inherit|embedded=yes|empty=yes|size=yes|data=yes}} | ||
+ | {{dsc end}} | ||
+ | |||
+ | {{anchor|ctor}} | ||
+ | {{member|{{small|std::ranges::ref_view::}}ref_view|2= | ||
+ | {{ddcl|since=c++20| | ||
+ | template< /*different-from*/<ref_view> T > | ||
requires std::convertible_to<T, R&> && requires { _FUN(std::declval<T>()); } | requires std::convertible_to<T, R&> && requires { _FUN(std::declval<T>()); } | ||
− | constexpr ref_view(T&& t); | + | constexpr ref_view( T&& t ); |
}} | }} | ||
− | Initializes {{ | + | Initializes {{tti|r_}} with {{c|std::addressof(static_cast<R&>(std::forward<T>(t)))}}. |
− | + | {{c|/*different-from*/<T, U>}} is satisfied if and only if {{c|std::remove_cvref_t<T>}} and {{c|std::remove_cvref_t<U>}} are not the same type, and overloads of {{tti|_FUN}} are declared as {{c|1=void _FUN(R&); void _FUN(R&&) = delete;}}. | |
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | t | range to reference }} | + | {{par|t|range to reference}} |
{{par end}} | {{par end}} | ||
− | |||
}} | }} | ||
− | {{member | {{small|std::ranges::ref_view::}}base| | + | {{anchor|base}} |
− | {{ddcl | since=c++20 | | + | {{member|{{small|std::ranges::ref_view::}}base| |
+ | {{ddcl|since=c++20| | ||
constexpr R& base() const; | constexpr R& base() const; | ||
}} | }} | ||
− | Equivalent to {{c|return *r_;}} | + | Equivalent to {{c|return *r_;}}. |
}} | }} | ||
− | {{member | {{small|std::ranges::ref_view::}}begin| | + | {{anchor|begin}} |
− | {{ddcl | since=c++20 | | + | {{member|{{small|std::ranges::ref_view::}}begin| |
+ | {{ddcl|since=c++20| | ||
constexpr ranges::iterator_t<R> begin() const; | constexpr ranges::iterator_t<R> begin() const; | ||
}} | }} | ||
− | Equivalent to {{c|return ranges::begin(*r_);}} | + | Equivalent to {{c|return ranges::begin(*r_);}}. |
}} | }} | ||
− | {{member | {{small|std::ranges::ref_view::}}end| | + | {{anchor|end}} |
− | {{ddcl | since=c++20 | | + | {{member|{{small|std::ranges::ref_view::}}end| |
+ | {{ddcl|since=c++20| | ||
constexpr ranges::sentinel_t<R> end() const; | constexpr ranges::sentinel_t<R> end() const; | ||
}} | }} | ||
− | Equivalent to {{c|return ranges::end(*r_);}} | + | Equivalent to {{c|return ranges::end(*r_);}}. |
}} | }} | ||
− | {{member | {{small|std::ranges::ref_view::}}empty| | + | {{anchor|empty}} |
− | {{ddcl | since=c++20 | | + | {{member|{{small|std::ranges::ref_view::}}empty| |
+ | {{ddcl|since=c++20| | ||
constexpr bool empty() const | constexpr bool empty() const | ||
requires requires { ranges::empty(*r_); }; | requires requires { ranges::empty(*r_); }; | ||
}} | }} | ||
− | Equivalent to {{c|return ranges::empty(*r_);}} | + | Equivalent to {{c|return ranges::empty(*r_);}}. |
}} | }} | ||
− | {{member | {{small|std::ranges::ref_view::}}size| | + | {{anchor|size}} |
− | {{ddcl | since=c++20 | | + | {{member|{{small|std::ranges::ref_view::}}size| |
+ | {{ddcl|since=c++20| | ||
constexpr auto size() const | constexpr auto size() const | ||
− | requires ranges::sized_range<R> | + | requires ranges::sized_range<R>; |
− | + | ||
}} | }} | ||
+ | |||
+ | Equivalent to {{c|return ranges::size(*r_);}}. | ||
}} | }} | ||
− | {{member | {{small|std::ranges::ref_view::}}data| | + | {{anchor|data}} |
− | {{ddcl | since=c++20 | | + | {{member|{{small|std::ranges::ref_view::}}data| |
+ | {{ddcl|since=c++20| | ||
constexpr auto data() const | constexpr auto data() const | ||
− | requires ranges::contiguous_range<R> | + | requires ranges::contiguous_range<R>; |
− | + | ||
− | + | ||
}} | }} | ||
− | {{ | + | Equivalent to {{c|return ranges::data(*r_);}}. |
+ | }} | ||
===Deduction guides=== | ===Deduction guides=== | ||
− | {{ddcl | since=c++20 | | + | {{ddcl|since=c++20| |
− | template<class R> | + | template< class R > |
− | ref_view(R&) -> ref_view<R>; | + | ref_view( R& ) -> ref_view<R>; |
}} | }} | ||
===Helper templates=== | ===Helper templates=== | ||
− | {{ddcl | since=c++20 | 1= | + | {{ddcl|since=c++20|1= |
− | template<class T> | + | template< class T > |
− | + | constexpr bool enable_borrowed_range<ranges::ref_view<T>> = true; | |
}} | }} | ||
− | This specialization of {{ | + | This specialization of {{rlpt|borrowed_range|std::ranges::enable_borrowed_range}} makes {{tt|ref_view}} satisfy {{lconcept|borrowed_range}}. |
===Example=== | ===Example=== | ||
− | {{example|code= | + | {{example |
− | + | |code= | |
#include <iostream> | #include <iostream> | ||
+ | #include <ranges> | ||
int main() | int main() | ||
Line 115: | Line 136: | ||
<< "call size() : " << rv.size() << '\n' | << "call size() : " << rv.size() << '\n' | ||
<< "call begin() : " << *rv.begin() << '\n' | << "call begin() : " << *rv.begin() << '\n' | ||
− | << "call end() : " << *(rv.end()-1) << '\n' | + | << "call end() : " << *(rv.end() - 1) << '\n' |
<< "call data() : " << rv.data() << '\n' | << "call data() : " << rv.data() << '\n' | ||
<< "call base() : " << rv.base().size() << '\n' // ~> tv.size() | << "call base() : " << rv.base().size() << '\n' // ~> tv.size() | ||
<< "range-for : "; | << "range-for : "; | ||
− | for (const auto c: rv) | + | for (const auto c : rv) |
+ | std::cout << c; | ||
std::cout << '\n'; | std::cout << '\n'; | ||
} | } | ||
Line 140: | Line 162: | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/utility/functional/dsc reference_wrapper}} | + | {{dsc inc|cpp/utility/functional/dsc reference_wrapper}} |
− | {{dsc inc | cpp/ranges/dsc all_view}} | + | {{dsc inc|cpp/ranges/dsc owning_view}} |
+ | {{dsc inc|cpp/ranges/dsc all_view}} | ||
{{dsc end}} | {{dsc end}} | ||
− | {{langlinks|es|ja|ru|zh}} | + | {{langlinks|de|es|ja|ru|zh}} |
Latest revision as of 22:57, 20 June 2024
Defined in header <ranges>
|
||
template< ranges::range R > requires std::is_object_v<R> |
(since C++20) | |
ref_view
is a view
of the elements of some other range
. It wraps a reference to that range
.
Contents |
[edit] Data members
Member name | Definition |
r_ (private)
|
A pointer of type R* to the underlying range.(exposition-only member object*) |
[edit] Member functions
constructs a ref_view that references to the given range (public member function) | |
returns the references to the referenced range (public member function) | |
returns the beginning iterator of the referenced range (public member function) | |
returns the sentinel of the referenced range (public member function) | |
checks whether the referenced range is empty (public member function) | |
returns the size of the referenced sized_range (public member function) | |
returns the pointer to the beginning of the referenced contiguous_range (public member function) | |
Inherited from std::ranges::view_interface | |
(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 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 if it satisfies bidirectional_range and common_range . (public member function of std::ranges::view_interface<D> )
| |
returns the n th element in the derived view. Provided if it satisfies random_access_range . (public member function of std::ranges::view_interface<D> )
|
std::ranges::ref_view::ref_view
template< /*different-from*/<ref_view> T > requires std::convertible_to<T, R&> && requires { _FUN(std::declval<T>()); } |
(since C++20) | |
Initializes r_
with std::addressof(static_cast<R&>(std::forward<T>(t))).
/*different-from*/<T, U> is satisfied if and only if std::remove_cvref_t<T> and std::remove_cvref_t<U> are not the same type, and overloads of _FUN
are declared as void _FUN(R&); void _FUN(R&&) = delete;.
Parameters
t | - | range to reference |
std::ranges::ref_view::base
constexpr R& base() const; |
(since C++20) | |
Equivalent to return *r_;.
std::ranges::ref_view::begin
constexpr ranges::iterator_t<R> begin() const; |
(since C++20) | |
Equivalent to return ranges::begin(*r_);.
std::ranges::ref_view::end
constexpr ranges::sentinel_t<R> end() const; |
(since C++20) | |
Equivalent to return ranges::end(*r_);.
std::ranges::ref_view::empty
constexpr bool empty() const requires requires { ranges::empty(*r_); }; |
(since C++20) | |
Equivalent to return ranges::empty(*r_);.
std::ranges::ref_view::size
constexpr auto size() const requires ranges::sized_range<R>; |
(since C++20) | |
Equivalent to return ranges::size(*r_);.
std::ranges::ref_view::data
constexpr auto data() const requires ranges::contiguous_range<R>; |
(since C++20) | |
Equivalent to return ranges::data(*r_);.
[edit] Deduction guides
template< class R > ref_view( R& ) -> ref_view<R>; |
(since C++20) | |
[edit] Helper templates
template< class T > constexpr bool enable_borrowed_range<ranges::ref_view<T>> = true; |
(since C++20) | |
This specialization of std::ranges::enable_borrowed_range
makes ref_view
satisfy borrowed_range
.
[edit] Example
#include <iostream> #include <ranges> int main() { const std::string s{"cosmos"}; const std::ranges::take_view tv{s, 3}; const std::ranges::ref_view rv{tv}; std::cout << std::boolalpha << "call empty() : " << rv.empty() << '\n' << "call size() : " << rv.size() << '\n' << "call begin() : " << *rv.begin() << '\n' << "call end() : " << *(rv.end() - 1) << '\n' << "call data() : " << rv.data() << '\n' << "call base() : " << rv.base().size() << '\n' // ~> tv.size() << "range-for : "; for (const auto c : rv) std::cout << c; std::cout << '\n'; }
Output:
call empty() : false call size() : 3 call begin() : c call end() : s call data() : cosmos call base() : 3 range-for : cos
[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 |
---|---|---|---|
P2325R3 | C++20 | default constructor was provided as view must be default_initializable
|
removed along with the requirement |
[edit] See also
(C++11) |
CopyConstructible and CopyAssignable reference wrapper (class template) |
(C++20) |
a view with unique ownership of some range (class template) |
(C++20) |
a view that includes all elements of a range (alias template) (range adaptor object) |