Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/ranges/contiguous range"

From cppreference.com
< cpp‎ | ranges
m (fmt)
m (+ more cases [example])
 
(One intermediate revision by one user not shown)
Line 4: Line 4:
 
{{ddcl|header = ranges|since=c++20|1=
 
{{ddcl|header = ranges|since=c++20|1=
 
template< class T >
 
template< class T >
  concept contiguous_range =
+
concept contiguous_range =
 
     ranges::random_access_range<T> &&
 
     ranges::random_access_range<T> &&
 
     std::contiguous_iterator<ranges::iterator_t<T>> &&
 
     std::contiguous_iterator<ranges::iterator_t<T>> &&
 
     requires(T& t) {
 
     requires(T& t) {
      { ranges::data(t) } ->
+
        { ranges::data(t) } ->
        std::same_as<std::add_pointer_t<ranges::range_reference_t<T>>>;
+
            std::same_as<std::add_pointer_t<ranges::range_reference_t<T>>>;
 
     };
 
     };
 
}}
 
}}
  
The {{c|contiguous_range}} concept is a refinement of {{lconcept|range}} for which {{lc|ranges::begin}} returns a model of {{lconcept|contiguous_iterator}} and the customization point {{c|ranges::data}} is usable.
+
The {{tt|contiguous_range}} concept is a refinement of {{lconcept|range}} for which {{lc|ranges::begin}} returns a model of {{lconcept|contiguous_iterator}} and the customization point {{lc|ranges::data}} is usable.
  
 
===Semantic requirements===
 
===Semantic requirements===
{{tt|T}} models {{c|contiguous_range}} only if given an expression {{tt|e}} such that {{c|decltype((e))}} is {{c|T&}}, {{c|1=std::to_address(ranges::begin(e)) == ranges::data(e)}}.
+
{{tt|T}} models {{tt|contiguous_range}} only if given an expression {{tt|e}} such that {{c|decltype((e))}} is {{c|T&}}, {{c|1=std::to_address(ranges::begin(e)) == ranges::data(e)}}.
  
 
===Example===
 
===Example===
Line 24: Line 24:
 
#include <deque>
 
#include <deque>
 
#include <list>
 
#include <list>
 +
#include <mdspan>
 
#include <ranges>
 
#include <ranges>
 
#include <set>
 
#include <set>
 +
#include <span>
 +
#include <string_view>
 
#include <valarray>
 
#include <valarray>
 
#include <vector>
 
#include <vector>
  
template<typename T> concept CR = std::ranges::contiguous_range<T>;
+
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 main()
Line 42: Line 61:
 
         not CR<std::list<int>> and
 
         not CR<std::list<int>> and
 
         not CR<std::set<int>> and
 
         not CR<std::set<int>> and
             CR<std::array<std::list<int>,42>>
+
             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>>>
 
     );
 
     );
 
}
 
}

Latest revision as of 00:59, 3 November 2024

 
 
Ranges library
Range adaptors
 
Defined in header <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>>>;

    };
(since C++20)

The contiguous_range concept is a refinement of range for which ranges::begin returns a model of contiguous_iterator and the customization point ranges::data is usable.

[edit] Semantic requirements

T models contiguous_range only if given an expression e such that decltype((e)) is T&, std::to_address(ranges::begin(e)) == ranges::data(e).

[edit] Example

#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>>>
    );
}

[edit] See also

specifies that a range knows its size in constant time
(concept) [edit]
specifies a range whose iterator type satisfies random_access_iterator
(concept) [edit]