Files
2025-10-25 03:02:53 +03:00

4.6 KiB
Raw Permalink Blame History

[range.view]

25 Ranges library [ranges]

25.4 Range requirements [range.req]

25.4.5 Views [range.view]

1

#

The view concept specifies the requirements of a range type that has the semantic properties below, which make it suitable for use in constructing range adaptor pipelines ([range.adaptors]).

🔗

template<class T> concept [view](#concept:view "25.4.5Views[range.view]") = [range](range.range#concept:range "25.4.2Ranges[range.range]")<T> && [movable](concepts.object#concept:movable "18.6Object concepts[concepts.object]")<T> && enable_view<T>;

2

#

T models view only if

T has O(1) move construction; and

move assignment of an object of type T is no more complex than destruction followed by move construction; and

if N copies and/or moves are made from an object of type T that contained M elements, then those N objects have O(N+M) destruction; and

copy_constructible is false, orT has O(1) copy construction; and

copyable is false, or copy assignment of an object of type T is no more complex than destruction followed by copy construction.

3

#

[Note 1:

The constraints on copying and moving imply that a moved-from object of type T has O(1) destruction.

— end note]

4

#

[Example 1:

Examples of views are:

  • (4.1)

    A range type that wraps a pair of iterators.

  • (4.2)

    A range type that holds its elements by shared_ptr and shares ownership with all its copies.

  • (4.3)

    A range type that generates its elements on demand.

A container such as vector does not meet the semantic requirements of view since copying the container copies all of the elements, which cannot be done in constant time.

— end example]

5

#

Since the difference between range and view is largely semantic, the two are differentiated with the help of enable_view.

🔗

template<class T> constexpr bool is-derived-from-view-interface = see below; // exposition only template<class T> constexpr bool enable_view = [derived_from](concept.derived#concept:derived_from "18.4.3Concept derived_­from[concept.derived]")<T, view_base> || is-derived-from-view-interface<T>;

6

#

For a type T,is-derived-from-view-interface is true if and only ifT has exactly one public base class view_interface for some type U andT has no base classes of type view_interface for any other type V.

7

#

Remarks: Pursuant to [namespace.std], users may specialize enable_view to true for cv-unqualified program-defined types that model view, and false for types that do not.

Such specializations shall be usable in constant expressions ([expr.const]) and have type const bool.