Muokkaa

Jaa


view_interface class (C++ Standard Library)

The base class for the view classes in the std::ranges namespace. This class implements some of the interface for derived view types. Use this as the base class for your own view types to reduce the boilerplate you need to write.

Syntax

template<class Derived>
  requires std::is_class_v<Derived> && 
  std::same_as<Derived, std::remove_cv_t<Derived>>
class view_interface;

Template parameters

Derived
The type of the class that is deriving from this base class.

Members

Member functions Description
backC++20 Get the last element in the derived view.
dataC++20 Get a pointer to the first element in the derived view.
emptyC++20 Test whether the derived view is empty.
frontC++20 Get the first element in the derived view.
sizeC++20 Get the number of elements in the derived view.
Operators Description
operator[]C++20 Get the element at the specified position.
operator boolC++20 Test whether the derived view isn't empty.

Requirements

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

Namespace: std::ranges

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

back

Get the last element in the derived view.

constexpr auto back()
    requires ranges::bidirectional_range<Derived> &&
    ranges::common_range<Derived>;

constexpr auto back() const
    requires ranges::bidirectional_range<const Derived> && 
    ranges::common_range<const Derived>;

Parameters

None.

Return value

The last element in the derived view.

Remarks

The derived view must satisfy bidirectional_range and common_range.
The behavior of back() and front() are undefined for any empty view.

data

Get a pointer to the first element in the derived view.

constexpr auto data()
    requires std::contiguous_iterator<ranges::iterator_t<Derived>>;
constexpr auto data() const
    requires ranges::range<const Derived> &&
    std::contiguous_iterator<ranges::iterator_t<const Derived>>;

Parameters

None.

Return value

A pointer to the first element in the derived view.

Remarks

The iterator for the derived view must satisfy contiguous_iterator.

empty

Test whether the derived view is empty.

1) constexpr bool empty() requires ranges::forward_range<Derived>;
2) constexpr bool empty() const requires ranges::forward_range<const Derived>;

Parameters

None.

Return value

Returns true if the derived view has no elements. Otherwise, returns false.

Remarks

The derived view must satisfy std::ranges::forward_range.

front

Get the first element in the derived view.

constexpr auto front()
    requires ranges::forward_range<Derived>;
constexpr auto front() const
    requires ranges::forward_range<const Derived>;

Parameters

None.

Return value

The last element in the derived view.

Remarks

The derived view must satisfy forward_range.
The behavior of front() is undefined for std::ranges::empty_view.

size

Get the number of elements in the derived view.

constexpr auto size() requires ranges::forward_range<Derived> &&
    std::sized_sentinel_for<ranges::sentinel_t<Derived>,
    ranges::iterator_t<Derived>>;
constexpr auto size() const requires ranges::forward_range<const Derived> &&
    std::sized_sentinel_for<ranges::sentinel_t<const Derived>,
    ranges::iterator_t<const Derived>>;

Parameters

None.

Return value

The number of elements in the derived view.

Remarks

The iterator for the derived view must satisfy sized_sentinel_for.

operator[]

Get the element at the specified position.

template<ranges::random_access_range R = Derived>
constexpr decltype(auto) operator[](ranges::range_difference_t<R> pos);

template<ranges::random_access_range R = const Derived>
constexpr decltype(auto) operator[](ranges::range_difference_t<R> pos) const;

Parameters

pos
The position, relative to the beginning iterator, of the element to return.

Return value

The element at the specified position relative to the beginning iterator.

Remarks

The derived view must satisfy random_access_range.
The behavior of this operator is undefined for std::ranges::empty_view.

Example: operator[]

// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v{1, 2, 3, 4, 5};
    std::ranges::drop_view dv = std::views::drop(v, 2);

    for (auto e : dv)
    {
        std::cout << e << ' '; // 3 4 5
    }

    std::cout << "\ndv[1] = " << dv[1];
}
3 4 5
dv[1] = 4

view_interface::operator bool

Test whether the derived view isn't empty.

explicit constexpr operator bool();
explicit constexpr operator bool() const;

Parameters

None.

Return value

Returns false if the derived view has no elements (the view is empty). Otherwise, returns true (the view isn't empty).

Remarks

The iterator for the derived view must satisfy std::ranges::forward_iterator.
This operator is equivalent to !empty(). This makes it convenient to write if (someRange) {...} to test whether there's something in the range to operate on.
The behavior of this operator is undefined for std::ranges::empty_view.

Example: operator bool

// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v{1, 2, 3, 4, 5};
    std::ranges::filter_view fv = std::views::filter(v, [](int e) { return e > 3; });

    bool isNotEmpty = static_cast<bool>(fv);
    std::cout << "Has elements greater than 3: " << std::boolalpha << isNotEmpty << '\n' >>;
}
Has elements greater than 3: true

See also

<ranges>
ranges::begin()
ranges::data()
ranges::end()
ranges::empty()
ranges::size()
View classes