Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/ranges/ref view"

From cppreference.com
< cpp‎ | ranges
(+ view_interface)
(P2325R3)
Line 13: Line 13:
 
{{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}}.
  
===Helper templates===
+
{{tt|ref_view}} behaves as if it only stores {{c|R*}} member subobject to the referenced range. The member is called {{tt|''r_''}} here (the name is exposition-only).
{{ddcl|1=
+
template<class T>
+
inline constexpr bool enable_borrowed_range<ranges::ref_view<T>> = true;
+
}}
+
This specialization of {{ltt|cpp/ranges/borrowed_range|std::ranges::enable_borrowed_range}} makes {{tt|ref_view}} satisfy {{lconcept|borrowed_range}}.
+
 
+
===Data members===
+
{{member | {{small|std::ranges::ref_view::}}r_ |
+
{{dcl begin}}
+
{{dcl |1=
+
R* r_ = nullptr; /* exposition-only */
+
}}
+
{{dcl end}}
+
pointer to the referenced range
+
}}
+
  
 
===Member functions===
 
===Member functions===
 
{{member | {{small|std::ranges::ref_view::}}ref_view | 2=
 
{{member | {{small|std::ranges::ref_view::}}ref_view | 2=
{{dcl begin}}
+
{{ddcl | since=c++20 |
{{dcl | num=1 |1=
+
constexpr ref_view() noexcept = default;
+
}}
+
{{dcl | num=2 |1=
+
 
template<__NotSameAs<ref_view> T>
 
template<__NotSameAs<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);
 
}}
 
}}
{{dcl end}}
 
  
@1@ Initializes {{tt|r_}} with {{c|nullptr}}. A default-initialized {{tt|ref_view}} references no {{lconcept|range}}.
+
Initializes {{tt|''r_''}} with {{c|std::addressof(static_cast<R&>(std::forward<T>(t)))}}.
@2@ Initializes {{tt|r_}} with {{c|std::addressof(static_cast<R&>(std::forward<T>(t)))}}.
+
  
Names {{tt|__NotSameAs}} and {{tt|_FUN}} are exposition-only. {{tt|__NotSameAs<T, U>}} is satisfied if and only if {{c|!std::same_as<std::remove_cvref_t<T>, std::remove_cvref_t<U>>}}. The function {{tt|_FUN}} are declared as {{c|1=void _FUN(R&); void _FUN(R&&) = delete;}}.
+
Names {{tt|''__NotSameAs''}} and {{tt|''_FUN''}} are exposition-only. {{c|__NotSameAs<T, U>}} is satisfied if and only if {{c|!std::same_as<std::remove_cvref_t<T>, std::remove_cvref_t<U>>}}. The function {{tt|''_FUN''}} are declared as {{c|1=void _FUN(R&); void _FUN(R&&) = delete;}}.
  
 
===Parameters===
 
===Parameters===
Line 56: Line 35:
  
 
{{member | {{small|std::ranges::ref_view::}}base|
 
{{member | {{small|std::ranges::ref_view::}}base|
{{dcl begin}}
+
{{ddcl | since=c++20 |
{{dcl |
+
 
constexpr R& base() const;
 
constexpr R& base() const;
 
}}
 
}}
{{dcl end}}
 
  
 
Equivalent to {{c|return *r_;}}
 
Equivalent to {{c|return *r_;}}
Line 66: Line 43:
  
 
{{member | {{small|std::ranges::ref_view::}}begin|
 
{{member | {{small|std::ranges::ref_view::}}begin|
{{dcl begin}}
+
{{ddcl | since=c++20 |
{{dcl |
+
 
constexpr ranges::iterator_t<R> begin() const;
 
constexpr ranges::iterator_t<R> begin() const;
 
}}
 
}}
{{dcl end}}
 
  
 
Equivalent to {{c|return ranges::begin(*r_);}}
 
Equivalent to {{c|return ranges::begin(*r_);}}
Line 76: Line 51:
  
 
{{member | {{small|std::ranges::ref_view::}}end|
 
{{member | {{small|std::ranges::ref_view::}}end|
{{dcl begin}}
+
{{ddcl | since=c++20 |
{{dcl |
+
 
constexpr ranges::sentinel_t<R> end() const;
 
constexpr ranges::sentinel_t<R> end() const;
 
}}
 
}}
{{dcl end}}
 
  
 
Equivalent to {{c|return ranges::end(*r_);}}
 
Equivalent to {{c|return ranges::end(*r_);}}
Line 86: Line 59:
  
 
{{member | {{small|std::ranges::ref_view::}}empty|
 
{{member | {{small|std::ranges::ref_view::}}empty|
{{dcl begin}}
+
{{ddcl | since=c++20 |
{{dcl |
+
 
constexpr bool empty() const
 
constexpr bool empty() const
 
     requires requires { ranges::empty(*r_); };
 
     requires requires { ranges::empty(*r_); };
 
}}
 
}}
{{dcl end}}
 
  
 
Equivalent to {{c|return ranges::empty(*r_);}}
 
Equivalent to {{c|return ranges::empty(*r_);}}
Line 97: Line 68:
  
 
{{member | {{small|std::ranges::ref_view::}}size|
 
{{member | {{small|std::ranges::ref_view::}}size|
{{dcl begin}}
+
{{ddcl | since=c++20 |
{{dcl |
+
 
constexpr auto size() const
 
constexpr auto size() const
 
     requires ranges::sized_range<R>
 
     requires ranges::sized_range<R>
 
{ return ranges::size(*r_); }
 
{ return ranges::size(*r_); }
 
}}
 
}}
{{dcl end}}
 
 
}}
 
}}
  
 
{{member | {{small|std::ranges::ref_view::}}data|
 
{{member | {{small|std::ranges::ref_view::}}data|
{{dcl begin}}
+
{{ddcl | since=c++20 |
{{dcl |
+
 
constexpr auto data() const
 
constexpr auto data() const
 
     requires ranges::contiguous_range<R>
 
     requires ranges::contiguous_range<R>
 
{ return ranges::data(*r_); }
 
{ return ranges::data(*r_); }
 
}}
 
}}
{{dcl end}}
 
 
}}
 
}}
  
Line 119: Line 86:
  
 
===Deduction guides===
 
===Deduction guides===
{{dcl begin}}
+
{{ddcl | since=c++20 |
{{dcl|
+
 
template<class R>
 
template<class R>
 
ref_view(R&) -> ref_view<R>;
 
ref_view(R&) -> ref_view<R>;
 
}}
 
}}
{{dcl end}}
+
 
 +
===Helper templates===
 +
{{ddcl | since=c++20 | 1=
 +
template<class T>
 +
inline constexpr bool enable_borrowed_range<ranges::ref_view<T>> = true;
 +
}}
 +
This specialization of {{ltt|cpp/ranges/borrowed_range|std::ranges::enable_borrowed_range}} makes {{tt|ref_view}} satisfy {{lconcept|borrowed_range}}.
  
 
===Example===
 
===Example===
Line 160: Line 132:
 
range-for    : cos
 
range-for    : cos
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|paper=P2325R3|std=C++20|before=default constructor was provided as {{lconcept|view}}<br>must be {{lconcept|default_initializable}}|after=removed along with the requirement}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===

Revision as of 05:08, 21 June 2021

 
 
Ranges library
Range adaptors
 
Defined in header <ranges>
template<ranges::range R>

    requires std::is_object_v<R>

class ref_view : public ranges::view_interface<ref_view<R>>
(since C++20)

ref_view is a view of the elements of some other range. It wraps a reference to that range.

ref_view behaves as if it only stores R* member subobject to the referenced range. The member is called r_ here (the name is exposition-only).

Contents

Member functions

std::ranges::ref_view::ref_view

template<__NotSameAs<ref_view> T>

    requires std::convertible_to<T, R&> && requires { _FUN(std::declval<T>()); }

constexpr ref_view(T&& t);
(since C++20)

Initializes r_ with std::addressof(static_cast<R&>(std::forward<T>(t))).

Names __NotSameAs and _FUN are exposition-only. __NotSameAs<T, U> is satisfied if and only if !std::same_as<std::remove_cvref_t<T>, std::remove_cvref_t<U>>. The function _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>

{ return ranges::size(*r_); }
(since C++20)

std::ranges::ref_view::data

constexpr auto data() const

    requires ranges::contiguous_range<R>

{ return ranges::data(*r_); }
(since C++20)

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>) [edit]
(C++23)
returns a sentinel for the constant iterator of the range.
(public member function of std::ranges::view_interface<D>) [edit]
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>) [edit]
returns the first element in the derived view. Provided if it satisfies forward_range.
(public member function of std::ranges::view_interface<D>) [edit]
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>) [edit]
returns the nth element in the derived view. Provided if it satisfies random_access_range.
(public member function of std::ranges::view_interface<D>) [edit]

Deduction guides

template<class R>
ref_view(R&) -> ref_view<R>;
(since C++20)

Helper templates

template<class T>
inline 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.

Example

#include <ranges>
#include <iostream>
 
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

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

See also

CopyConstructible and CopyAssignable reference wrapper
(class template) [edit]
a view that includes all elements of a range
(alias template) (range adaptor object)[edit]