std::ranges::view, std::ranges::enable_view, std::ranges::view_base

Header: <ranges>

  1. The view concept specifies the requirements of a range type that has suitable semantic properties for use in constructing range adaptor pipelines.

# Declarations

template<class T>
concept view = ranges::range<T> && std::movable<T> && ranges::enable_view<T>;

(since C++20)

template<class T>
constexpr bool enable_view =
std::derived_from<T, view_base> || /*is-derived-from-view-interface*/<T>;

(since C++20)

struct view_base { };

(since C++20)

# Notes

Examples of view types are:

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.

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

#include <ranges>
 
struct ArchetypalView : std::ranges::view_interface<ArchetypalView>
{
    int* begin();
    int* end();
};
 
static_assert(std::ranges::view<ArchetypalView>);

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
P2325R3C++20view required default_initializabledoes not require
LWG 3549C++20enable_view did not detect inheritance from view_interfacedetects
P2415R2C++20the restriction on the time complexity of destruction was too strictrelaxed