名前空間
変種
操作

std::ranges::range

From cppreference.com
< cpp‎ | ranges
 
 
Rangesライブラリ
Rangeアダプタ
 
ヘッダ <ranges> で定義
template< class T >

concept range = requires( T& t ) {
    ranges::begin(t); // equality-preserving for forward iterators
    ranges::end (t);

};
(C++20以降)

The range concept defines the requirements of a type that allows iteration over its elements by providing an iterator and sentinel that denote the elements of the range.

目次

[編集] 意味的要件

Given an expression E such that decltype((E)) is T, T models range only if

[編集] 注記

A typical range class only needs to provide two functions

  1. A member function begin() whose return type models input_or_output_iterator.
  2. A member function end() whose return type models sentinel_for<It>, where It is the return type of begin().

Alternatively, they can be non-member functions, to be found by argument-dependent lookup.

[編集]

#include <ranges>
 
// A minimum range
struct SimpleRange
{
    int* begin();
    int* end();
};
static_assert(std::ranges::range<SimpleRange>);
 
// Not a range: no begin/end
struct NotRange
{
    int t {};
};
static_assert(!std::ranges::range<NotRange>);
 
// Not a range: begin does not return an input_or_output_iterator
struct NotRange2
{
    void* begin();
    int* end();
};
static_assert(!std::ranges::range<NotRange2>);
 
int main() {}

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 3915 C++20 ranges::begin(t) and ranges::end(t)
did not require implicit expression variations
冗長な要件が
redundant description
English 日本語 中文(简体) 中文(繁體)