namespace std::ranges::views {
inline constexpr /*unspecified*/ counted = /*unspecified*/;
}
概要
イテレータiと数nに対して、範囲[i, i + n)をRangeとして扱うviewを生成するカスタマイゼーションポイントオブジェクト。実際に生成するのはsubrangeかspanである。
countedは、手元にイテレータだけがあるとき、そこから指定個数までの範囲をRangeとして扱うために使用できる。これは、ポインタと個数で定まる範囲をcontiguous_rangeとして扱うspanを、一般のイテレータに拡張したものである。
countedはRangeアダプタオブジェクトではないので、パイプライン記法は使用できない。
Rangeコンセプト
| borrowed | sized | output | input | forward | bidirectional | random_access | contiguous | common | viewable | view |
|---|---|---|---|---|---|---|---|---|---|---|
| ○ | ○ | ※ | ※ | ※ | ※ | ※ | ※ | ※ | ○ | ○ |
※ イテレータに従う
効果
式Eをイテレータ、式Fを進める数、イテレータ型Tをdecay_t<decltype((E))>、イテレータの差の型Dをiter_difference_t<T>とする。式views::counted(E, F)の効果は以下の通り。
decltype((F))がconvertible_to<D>のモデルでなければ、呼び出しは不適格。Tがcontiguous_iteratorのモデルであれば、span(to_address(E), static_cast<D>(F))と等しい。Tがrandom_access_iteratorのモデルであれば、subrange(E, E + static_cast<D>(F))と等しい。- それ以外のとき、
subrange(counted_iterator(E, F), default_sentinel)と等しい。
例
#include <ranges>
#include <iterator>
#include <vector>
#include <iostream>
int main() {
using namespace std;
vector<int> v;
for (int i = 0; auto& elem : views::counted(back_inserter(v), 5)) {
elem = i++;
}
for(int elem : v) {
cout << elem;
}
}
出力
01234
バージョン
言語
- C++20
処理系
- Clang: 13.0.0 ✅
- GCC: 10.1.0 ✅
- ICC: ?
- Visual C++: 2019 Update 10 ✅
参照
- N4861 24 Ranges library
- C++20 ranges
- P2367R0 Remove misuses of list-initialization from Clause 24 (本提案文書はC++20に遡って適用されている)