std::ranges::views::common, std::ranges::common_view
From cppreference.com
| ヘッダ <ranges> で定義 |
||
| template< ranges::view V > requires (not ranges::common_range<V> and |
(1) | (C++20以降) |
| namespace views { inline constexpr /* 不明 */ common = /* 不明 */; |
(2) | (C++20以降) |
| 呼び出しシグネチャ |
||
| template< ranges::viewable_range R > requires /* 以下を参照 */ |
(C++20以降) | |
2) RangeAdaptorObject。 e をサブ式とすると、式 views::common(e) は、それがwell-formedな式であり、かつ decltype((e)) が
common_range をモデルとする場合、 expression-equivalent です。- views::all(e)、そうでなければ
- common_view{e}。
目次 |
[編集] データメンバー
| メンバ | 説明 |
V base_ (private) |
基盤となるビュー (説明用のメンバオブジェクト*) |
[編集] メンバー関数
common_viewを構築します。(public メンバ関数) | |
| 基になる(適応された)ビューのコピーを返す (public member function) | |
| 先頭へのイテレータを返す (public メンバ関数) | |
| 末尾へのイテレータを返す (public メンバ関数) | |
基底(アダプトされた)レンジが sized_range を満たす場合にのみ、要素数を返す(public メンバ関数) | |
std::ranges::view_interface から継承 | |
派生ビューが空かどうかを返す。sized_range または forward_range を満たす場合にのみ提供される( std::ranges::view_interface<D> の public メンバ関数) | |
| (C++23) |
範囲の先頭への定数イテレータを返す ( std::ranges::view_interface<D> の public メンバ関数) |
| (C++23) |
範囲の定数イテレータの番兵を返す ( std::ranges::view_interface<D> の public メンバ関数) |
| 派生ビューが空でないかどうかを返す。ranges::empty が適用可能な場合にのみ提供される ( std::ranges::view_interface<D> の public メンバ関数) | |
イテレータ型が contiguous_iterator を満たす場合にのみ、派生ビューのデータのアドレスを取得する( std::ranges::view_interface<D> の公開メンバ関数) | |
派生ビューが forward_range を満たす場合に、派生ビューの最初の要素を返す( std::ranges::view_interface<D> の public メンバ関数) | |
派生ビューが bidirectional_range と common_range の両方を満たす場合にのみ、派生ビューの最後の要素を返す( std::ranges::view_interface<D> の public メンバ関数) | |
派生ビューが random_access_range を満たす場合にのみ、派生ビューの n番目の要素を返す( std::ranges::view_interface<D> の public メンバ関数) | |
[編集] 推論ガイド
[編集] ヘルパーテンプレート
| template< class T > constexpr bool enable_borrowed_range<std::ranges::common_view<T>> = |
(C++20以降) | |
std::enable_borrowed_range のこの特殊化により、基になるビューがborrowed_rangeをモデルとする場合、common_viewはborrowed_rangeを満たします。
[編集] 注釈
common_viewは、イテレータとセンチネルが同じ型であることを期待するレガシーアルゴリズムを扱う場合に役立ちます。
[編集] 例
このコードを実行
#include <iostream> #include <iterator> #include <list> #include <numeric> #include <ranges> int main() { auto v1 = {1, 2, 3, 4, 5}; auto i1 = std::counted_iterator{v1.begin(), std::ssize(v1)}; auto r1 = std::ranges::subrange{i1, std::default_sentinel}; // auto e1 = std::accumulate(r1.begin(), r1.end(), 0); // error: "common range" required auto c1 = std::ranges::common_view{r1}; std::cout << "accumulate: " << std::accumulate(c1.begin(), c1.end(), 0) << '\n'; // inherited from ranges::view_interface: std::cout << "c1.front(): " << c1.front() << '\n'; std::cout << "c1.back(): " << c1.back() << '\n'; std::cout << "c1.data(): " << c1.data() << '\n'; std::cout << "c1[0]: " << c1[0] << '\n'; auto v2 = std::list{1, 2, 3, 4, 5}; auto i2 = std::counted_iterator{v2.begin(), std::ssize(v2)}; auto r2 = std::ranges::subrange{i2, std::default_sentinel}; // auto e2 = std::accumulate(r2.begin(), r2.end(), 0); // error: "common range" required auto c2 = std::ranges::common_view{ r2 }; std::cout << "accumulate: " << std::accumulate(c2.begin(), c2.end(), 0) << '\n'; // inherited from ranges::view_interface: std::cout << "c2.front(): " << c2.front() << '\n'; // auto e3 = c2.back(); // error: "bidirectional range" required // auto e4 = c2.data(); // error: "contiguous range" required // auto e5 = c2[0]; // error: "random access range" required }
実行結果の例
accumulate: 15 c1.front(): 1 c1.back(): 5 c1.data(): 0x7f19937f00d0 c1[0]: 1 accumulate: 15 c2.front(): 1
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 3494 | C++20 | common_view はかつて borrowed_range ではなかった |
基になるビューがそうであれば、borrowed_range になる |
[編集] 関連項目
| (C++20) |
rangeが同一のイテレータ型と番兵型を持つことを規定する (コンセプト) |
| (C++20) |
イテレータ型とその番兵 (sentinel) を共通のイテレータ型に適合させる (クラステンプレート) |