Spec-Zone.ru › C++

std::ranges::contiguous_range

Определён в заголовочном файле <ranges>
template< class T >
concept contiguous_range =
    ranges::random_access_range<T> &&
    std::contiguous_iterator<ranges::iterator_t<T>> &&
    requires(T& t) {
        { ranges::data(t) } ->
            std::same_as<std::add_pointer_t<ranges::range_reference_t<T>>>;
    };
(начиная с C++20)

Концепт contiguous_range является уточнением range, для которого ranges::begin возвращает модель contiguous_iterator, а точка настройки ranges::data доступна.

Семантические требования

T моделирует contiguous_range только если для данного выражения e, такого что decltype((e)) является T&, выполняется std::to_address(ranges::begin(e)) == ranges::data(e).

Пример

#include <array>
#include <deque>
#include <list>
#include <mdspan>
#include <ranges>
#include <set>
#include <span>
#include <string_view>
#include <valarray>
#include <vector>

template<typename T>
concept CR = std::ranges::contiguous_range<T>;

// zstring being a ranges::contiguous_range doesn't have to be a ranges::sized_range
struct zstring
{
    struct sentinel
    {
        friend constexpr bool operator==(const char* str, sentinel) noexcept
        { return *str == '\0'; }
    };

    const char* str;

    const char* begin() const noexcept { return str; }
    sentinel end() const noexcept { return {}; }
};

int main()
{
    int a[4];
    static_assert(
            CR<std::vector<int>> and
        not CR<std::vector<bool>> and
        not CR<std::deque<int>> and
            CR<std::valarray<int>> and
            CR<decltype(a)> and
        not CR<std::list<int>> and
        not CR<std::set<int>> and
            CR<std::array<std::list<int>,42>> and
            CR<std::string_view> and
            CR<zstring> and
            CR<std::span<const int>> and
        not CR<std::mdspan<int, std::dims<1>>>
    );
}

См. также

ranges::sized_range
(C++20)
задаёт диапазон, размер которого можно получить за постоянное время
(концепт)
ranges::random_access_range
(C++20)
задаёт диапазон, тип итератора которого удовлетворяет требованиям random_access_iterator
(концепт)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/ranges/contiguous_range

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API