Namespaces
Variants

std::ranges:: contiguous_range

From cppreference.net
Ranges library
Range adaptors
定义于头文件 <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 可用。

语义要求

当且仅当给定表达式 e 满足 decltype ( ( e ) ) T & 时, T 才满足 contiguous_range 概念,且需满足 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 作为 ranges::contiguous_range 不必是 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>>>
    );
}

参见

指定能在常数时间内获知自身大小的范围
(概念)
指定其迭代器类型满足 random_access_iterator 的范围
(概念)