Rediger

Del via


common_view class (C++ Standard Library)

Take a range that may have different iterator and sentinel types and create a view that has the same iterator and sentinel type. This is useful for calling STL algorithms that accept ranges specified by iterator pairs.

Syntax

template<ranges::view V>
    requires (!ranges::common_range<V> && std::copyable<ranges::iterator_t<V>>)
class common_view : public ranges::view_interface<common_view<V>>;

Template parameters

V
The type of the underlying view.

View characteristics

For a description of the following entries, see View class characteristics

Characteristic Description
Range adaptor views::common
Underlying range Must satisfy forward_range or higher
Element type Same as the underlying range
View iterator category forward_range or random_access_range when the underlying range satisfies random_access_range and sized_range
Sized Only if the underlying range satisfies sized_range
Is const-iterable Only if the underlying range is const iterable
Common range Yes
Borrowed range Only if the underlying range satisfies borrowed_range

Members

Member functions Description
ConstructorsC++20 Construct a common_view.
baseC++20 Get the underlying view.
begin C++20 Get an iterator to the first element in the view.
endC++20 Get the sentinel at the end of the view.
sizeC++20 Get the number of elements in the view.
Inherited from view_interface Description
backC++20 Get the last element.
dataC++20 Get a pointer to the first element.
emptyC++20 Test whether the view is empty.
frontC++20 Get the first element.
operator[]C++20 Get the element at the specified position.
operator boolC++20 Test whether the view isn't empty.

Remarks

The best way to create a common_view is by using the views::common range adaptor. Range adaptors are the intended way to create view classes. The view types are exposed in case you want to create your own custom view type.

This view is useful for passing a range that has different iterator/sentinel types to a legacy algorithm that expects them to be the same.

Requirements

Header: <ranges> (since C++20)

Namespace: std::ranges

Compiler Option: /std:c++20 or later is required.

Constructors

Create an instance of a common_view.

1) common_view() = default;
2) constexpr explicit common_view(V v);

Parameters

v
The underlying view.

For information about the template parameter type, see Template parameters.

Remarks

1) Default constructs the common_view.
2) Constructs a common_view from the underlying view using std::move(v). An error will result if V is a common range to avoid misuse that would negatively impact performance.

base

Gets a copy of the underlying view.

// Uses a copy constructor to return the underlying view
constexpr V base() const& requires std::copy_constructible<V>;

// Uses a move constructor to return the underlying view
constexpr V base() &&;

begin

Get an iterator to the first element.

constexpr auto begin();
constexpr auto begin() const requires range<const V>;

Return value

An iterator pointing at the first element in the view:

Picture of a vector with the elements 10, 20, and 30. The first element contains 10 and is labeled begin(). The last element contains 30 and is labeled 'last element'. An imaginary box after the last element indicates the sentinel and is labeled end().

end

Get the sentinel at the end of the view.

constexpr auto end();
constexpr auto end() const requires ranges::range<const V>;

Return value

The sentinel that follows the last element in the view:

Picture of a vector with the elements 10, 20, and 30. The first element contains 10 and is labeled begin(). The last element contains 30 and is labeled 'last element'. An imaginary box after the last element indicates the sentinel and is labeled end().

size

Get the number of elements in the view.

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

Parameters

None.

Return value

The number of elements in the view.

See also

<ranges>
common range adaptor
view classes