std::ranges...s::view_base(3)
okay bestie, you just opened the std::ranges...s::view_base(3) page from the C++ Standard Libary and we're gonna make it make sense π this is the group chat where view, enable_view, and view_base all get their moment.
π NAME π
std::ranges::view,std::ranges::enable_view,std::ranges::view_base β and the one-liner is literally std::ranges::view,std::ranges::enable_view,std::ranges::view_base π
the iconic trio, no cap.
β¨ Synopsis β¨
Defined in header <ranges> β put that in your notes, bestie π
Defined in header <ranges>
template<class T>
concept view = ranges::range<T> && std::movable<T> &&
(1) (since C++20)
ranges::enable_view<T>;
template<class T>
inline constexpr bool enable_view =
(2) (since C++20)
std::derived_from<T, view_base> ||
/*is-derived-from-view-interface*/<T>;
struct view_base { };
(3) (since C++20)
- The
viewconcept specifies the requirements of a range type that has suitable semantic properties for use in constructing range adaptor pipelines. (since C++20) π¦ - The
enable_viewvariable template is used to indicate whether a range is aview. (since C++20)/*is-derived-from-view-interface*/<T>is true if and only ifThas exactly one public base classranges::view_interface<U>for some typeU, andThas no base classes of typeranges::view_interface<V>for any other typeV. Users may specializeenable_viewtotruefor cv-unqualified program-defined types which modelview, andfalsefor types which do not. Such specializations must be usable in constant expressions and have typeconst bool. π - Deriving from
view_baseenables range types to modelview. (since C++20) β¨
π¦ Notes π¦
Examples of view types are:
- A range type that wraps a pair of iterators, e.g.,
std::ranges::subrange<I>. - A range type that holds its elements by
std::shared_ptrand shares ownership with all its copies. - A range type that generates its elements on demand, e.g.,
std::ranges::iota_view.
A copyable container such as std::vector<std::string> generally does not meet the semantic requirements of view since copying the container copies all of the elements, which cannot be done in constant time. π
While views were originally described as cheaply copyable and non-owning ranges, a type is not required to be copyable or non-owning for it to model view. However, it must still be cheap to copy (if it is copyable), move, assign, and destroy, so that range adaptors will not have unexpected complexity. fr fr π«Ά
By default, a type modeling movable and range is considered a view if it is publicly and unambiguously derived from view_base, or exactly one specialization of std::ranges::view_interface. π
π Example π
A minimum view.
#include <ranges>
struct ArchetypalView : std::ranges::view_interface<ArchetypalView>
{
int* begin();
int* end();
};
static_assert(std::ranges::view<ArchetypalView>);
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
P2325R3β applied to C++20. Behavior as published:viewrequireddefault_initializable. Correct behavior: does not require.LWG 3549β applied to C++20. Behavior as published:enable_viewdid not detect inheritance fromview_interface. Correct behavior: detects.P2415R2β applied to C++20. Behavior as published: the restriction on the time complexity of destruction was too strict. Correct behavior: relaxed.
shoutout to http://cppreference.com β version 2024.06.10 β for std::ranges...s::view_base(3). mwah π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms