Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/ranges/as const view"

From cppreference.com
< cpp‎ | ranges
m (See also: +`std::as_const`; fmt)
Line 1: Line 1:
{{cpp/ranges/view title | as_const}}
+
{{cpp/ranges/view title|as_const}}
 
{{cpp/ranges/navbar}}
 
{{cpp/ranges/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | ranges}}
+
{{dcl header|ranges}}
{{dcl | num=1 | since=c++23 |1=
+
{{dcl|num=1|since=c++23|1=
 
template< ranges::view V >
 
template< ranges::view V >
 
     requires ranges::input_range<V>
 
     requires ranges::input_range<V>
class as_const_view : public ranges::view_interface<as_const_view<V>>
+
class as_const_view
 +
    : public ranges::view_interface<as_const_view<V>>
 
}}
 
}}
{{dcl | num=2 | since=c++23 |1=
+
{{dcl|num=2|since=c++23|1=
 
namespace views {
 
namespace views {
 
     inline constexpr /* unspecified */ as_const = /* unspecified */;
 
     inline constexpr /* unspecified */ as_const = /* unspecified */;
Line 14: Line 15:
 
}}
 
}}
 
{{dcl h|Call signature}}
 
{{dcl h|Call signature}}
{{dcl | since=c++23 |1=
+
{{dcl|since=c++23|1=
 
template< ranges::viewable_range R >
 
template< ranges::viewable_range R >
 
     requires /* see below */
 
     requires /* see below */
Line 34: Line 35:
  
 
===Data members===
 
===Data members===
Typical implementations of {{tt|as_const_view}} only hold one non-static data member:  
+
Typical implementations of {{tt|as_const_view}} only hold one non-static data member:
 
* the underlying view of type {{tt|V}} (shown here as {{tt|''base_''}}, the name is exposition only).
 
* the underlying view of type {{tt|V}} (shown here as {{tt|''base_''}}, the name is exposition only).
  
 
===Member functions===
 
===Member functions===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc mem ctor | nolink=true | constructs an {{tt|as_const_view}} | notes={{mark c++23}} }}
+
{{dsc mem ctor|nolink=true|constructs an {{tt|as_const_view}}|notes={{mark c++23}}}}
{{dsc mem fun | nolink=true | base | returns the underlying view {{tt|V}} | notes={{mark c++23}} }}
+
{{dsc mem fun|nolink=true|base|returns the underlying view {{tt|V}}|notes={{mark c++23}}}}
{{dsc mem fun | nolink=true | begin | returns the beginning iterator of the {{tt|as_const_view}} | notes={{mark c++23}} }}
+
{{dsc mem fun|nolink=true|begin|returns the beginning iterator of the {{tt|as_const_view}}|notes={{mark c++23}}}}
{{dsc mem fun | nolink=true | end | returns the end iterator of the {{tt|as_const_view}} | notes={{mark c++23}} }}
+
{{dsc mem fun|nolink=true|end|returns the end iterator of the {{tt|as_const_view}}|notes={{mark c++23}}}}
{{dsc mem fun | nolink=true | size | returns the size of the view if it is bounded | notes={{mark c++23}} }}
+
{{dsc mem fun|nolink=true|size|returns the size of the view if it is bounded|notes={{mark c++23}}}}
  
 
{{cpp/ranges/view_interface/inherit|embedded=yes|size=yes}}
 
{{cpp/ranges/view_interface/inherit|embedded=yes|size=yes}}
 
{{dsc end}}
 
{{dsc end}}
  
{{member | {{small|std::ranges::as_const_view::}}as_const_view |
+
{{member|{{small|std::ranges::as_const_view::}}as_const_view|
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | num=1 | since=c++23 |1=
+
{{dcl|num=1|since=c++23|1=
 
as_const_view() requires std::default_initializable<V> = default;
 
as_const_view() requires std::default_initializable<V> = default;
 
}}
 
}}
{{dcl | num=2 | since=c++23 |
+
{{dcl|num=2|since=c++23|
constexpr explicit as_const_view(V base);
+
constexpr explicit as_const_view( V base );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 63: Line 64:
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | base | a view }}
+
{{par|base|a view}}
 
{{par end}}
 
{{par end}}
 
}}
 
}}
  
{{member | {{small|std::ranges::as_const_view::}}base|
+
{{member|{{small|std::ranges::as_const_view::}}base|
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | num=1 | since=c++23 |
+
{{dcl|num=1|since=c++23|
 
constexpr V base() const& requires std::copy_constructible<V>;
 
constexpr V base() const& requires std::copy_constructible<V>;
 
}}
 
}}
{{dcl | num=2 | since=c++23 |
+
{{dcl|num=2|since=c++23|
 
constexpr V base() &&;
 
constexpr V base() &&;
 
}}
 
}}
Line 82: Line 83:
 
}}
 
}}
  
{{member | {{small|std::ranges::as_const_view::}}begin|
+
{{member|{{small|std::ranges::as_const_view::}}begin|
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | num=1 | since=c++23 |
+
{{dcl|num=1|since=c++23|
constexpr auto begin() requires (!/*simple-view*/<V>) {  
+
constexpr auto begin() requires (!__simple_view<V>) {
  return ranges::cbegin(base_);  
+
    return ranges::cbegin(base_);
 
}
 
}
 
}}
 
}}
{{dcl | num=2 | since=c++23 |
+
{{dcl|num=2|since=c++23|
constexpr auto begin() const requires ranges::range<const V> {  
+
constexpr auto begin() const requires ranges::range<const V> {
  return ranges::cbegin(base_);  
+
    return ranges::cbegin(base_);
 
}
 
}
 
}}
 
}}
Line 98: Line 99:
 
}}
 
}}
  
{{member | {{small|std::ranges::as_const_view::}}end|
+
{{member|{{small|std::ranges::as_const_view::}}end|
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | num=1 | since=c++23 |
+
{{dcl|num=1|since=c++23|
constexpr auto end() requires (!/*simple-view*/<V>) {  
+
constexpr auto end() requires (!__simple_view<V>) {
  return ranges::cend(base_);  
+
    return ranges::cend(base_);
 
}
 
}
 
}}
 
}}
{{dcl | num=2 | since=c++23 |
+
{{dcl|num=2|since=c++23|
constexpr auto end() const requires ranges::range<const V> {  
+
constexpr auto end() const requires ranges::range<const V> {
  return ranges::cend(base_);  
+
    return ranges::cend(base_);
 
}
 
}
 
}}
 
}}
Line 114: Line 115:
 
}}
 
}}
  
{{member | {{small|std::ranges::as_const_view::}}size|
+
{{member|{{small|std::ranges::as_const_view::}}size|
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | num=1 | since=c++23 |
+
{{dcl|num=1|since=c++23|
 
constexpr auto size() requires ranges::sized_range<V> {
 
constexpr auto size() requires ranges::sized_range<V> {
 
     return ranges::size(base_);
 
     return ranges::size(base_);
 
}
 
}
 
}}
 
}}
{{dcl | num=2 | since=c++23 |
+
{{dcl|num=2|since=c++23|
 
constexpr auto size() const requires ranges::sized_range<const V> {
 
constexpr auto size() const requires ranges::sized_range<const V> {
 
     return ranges::size(base_);
 
     return ranges::size(base_);
Line 131: Line 132:
  
 
===Deduction guides===
 
===Deduction guides===
{{ddcl | since=c++23 |
+
{{ddcl|since=c++23|
template<class R>
+
template< class R >
as_const_view(R&&) -> as_const_view<views::all_t<R>>;
+
as_const_view( R&& ) -> as_const_view<views::all_t<R>>;
 
}}
 
}}
  
 
===Helper templates===
 
===Helper templates===
 
{{ddcl|since=c++23|1=
 
{{ddcl|since=c++23|1=
template<class T>
+
template< class T >
 
inline constexpr bool enable_borrowed_range<std::ranges::as_const_view<T>> =
 
inline constexpr bool enable_borrowed_range<std::ranges::as_const_view<T>> =
 
     std::ranges::enable_borrowed_range<T>;
 
     std::ranges::enable_borrowed_range<T>;
Line 149: Line 150:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/ranges/dsc cbegin}}
+
{{dsc inc|cpp/ranges/dsc cbegin}}
{{dsc inc | cpp/ranges/dsc cend}}
+
{{dsc inc|cpp/ranges/dsc cend}}
 +
{{dsc inc|cpp/utility/dsc as_const}}
 
<!--
 
<!--
{{dsc inc | cpp/iterator/dsc basic_const_iterator}}
+
{{dsc inc|cpp/iterator/dsc basic_const_iterator}}
 
-->
 
-->
 
{{dsc end}}
 
{{dsc end}}

Revision as of 15:37, 18 January 2023

 
 
Ranges library
Range adaptors
 
Defined in header <ranges>
template< ranges::view V >

    requires ranges::input_range<V>
class as_const_view

    : public ranges::view_interface<as_const_view<V>>
(1) (since C++23)
namespace views {

    inline constexpr /* unspecified */ as_const = /* unspecified */;

}
(2) (since C++23)
Call signature
template< ranges::viewable_range R >

    requires /* see below */

constexpr ranges::view auto as_const( R&& r );
(since C++23)
1) A range adaptor that represents a view of underlying view that is also a constant_range. An as_const_view always has read-only elements (if not empty).
2) RangeAdaptorObject. Let e be a subexpression, let T be decltype((e)), and let U be std::remove_cvref_t<T>. Then the expression views::as_const(e) is expression-equivalent to:

as_const_view always models constant_range, and it models the contiguous_range, random_access_range, bidirectional_range, forward_range, borrowed_range, common_range, and sized_range when the underlying view V models respective concepts.

Contents

Expression-equivalent

Expression e is expression-equivalent to expression f, if

  • e and f have the same effects, and
  • either both are constant subexpressions or else neither is a constant subexpression, and
  • either both are potentially-throwing or else neither is potentially-throwing (i.e. noexcept(e) == noexcept(f)).

Data members

Typical implementations of as_const_view only hold one non-static data member:

  • the underlying view of type V (shown here as base_, the name is exposition only).

Member functions

(constructor)
(C++23)
constructs an as_const_view
(public member function)
base
(C++23)
returns the underlying view V
(public member function)
begin
(C++23)
returns the beginning iterator of the as_const_view
(public member function)
end
(C++23)
returns the end iterator of the as_const_view
(public member function)
size
(C++23)
returns the size of the view if it is bounded
(public member function)
Inherited from std::ranges::view_interface
returns whether the derived view is empty. Provided if it satisfies sized_range or forward_range.
(public member function of std::ranges::view_interface<D>) [edit]
(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]
gets the address of derived view's data. Provided if its iterator type satisfies contiguous_iterator.
(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]

std::ranges::as_const_view::as_const_view

as_const_view() requires std::default_initializable<V> = default;
(1) (since C++23)
constexpr explicit as_const_view( V base );
(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_const_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_const_view::begin

constexpr auto begin() requires (!__simple_view<V>) {

    return ranges::cbegin(base_);

}
(1) (since C++23)
constexpr auto begin() const requires ranges::range<const V> {

    return ranges::cbegin(base_);

}
(2) (since C++23)

Returns the constant iterator of the view.

std::ranges::as_const_view::end

constexpr auto end() requires (!__simple_view<V>) {

    return ranges::cend(base_);

}
(1) (since C++23)
constexpr auto end() const requires ranges::range<const V> {

    return ranges::cend(base_);

}
(2) (since C++23)

Returns the constant sentinel of the view.

std::ranges::as_const_view::size

constexpr auto size() requires ranges::sized_range<V> {

    return ranges::size(base_);

}
(1) (since C++23)
constexpr auto size() const requires ranges::sized_range<const V> {

    return ranges::size(base_);

}
(2) (since C++23)

Returns the size of the view if the view is bounded.

Deduction guides

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

Helper templates

template< class T >

inline constexpr bool enable_borrowed_range<std::ranges::as_const_view<T>> =

    std::ranges::enable_borrowed_range<T>;
(since C++23)

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

Example

See also

returns an iterator to the beginning of a read-only range
(customization point object)[edit]
returns a sentinel indicating the end of a read-only range
(customization point object)[edit]
(C++17)
obtains a reference to const to its argument
(function template) [edit]