Namespaces
Variants

std::ranges::views::common, std::ranges::common_view

ja.cppreference.net より
 
 
範囲ライブラリ
範囲アダプタ
 
 
ヘッダで定義 <ranges>
template< ranges::view V >
    requires (not ranges::common_range<V> and
              std::copyable<ranges::iterator_t<V>>)
class common_view
    : public ranges::view_interface<common_view<V>>
(1) (C++20以上)
namespace views {
    inline constexpr /* unspecified */ common = /* unspecified */;
}
(2) (C++20以上)
呼び出しシグネチャ
template< ranges::viewable_range R >
    requires /* see below */
constexpr ranges::view auto common( R&& r );
(C++20以上)
1) 与えられたview(イテレータ/センチネルペアの型が異なる)を、view でありながらcommon_rangeでもあるものに適合させます。common_view は常に同じイテレータ/センチネル型を持ちます。
2) RangeAdaptorObject。e を部分式とします。このとき、式views::common(e) は式と等価 であり、次に従います:
  • views::all(e),それが適格な式であり、かつdecltype((e)) がcommon_rangeのモデルである場合;
  • common_view{e} そうでない場合。

データメンバ

メンバー 説明
V base_ (private) 基となるビュー
( 説明専用メンバーオブジェクト* )

メンバー関数

common_viewを構築する
(公開メンバ関数)
基盤となる(アダプトされた)ビューのコピーを返す
(公開メンバ関数)
先頭へのイテレータを返す
(公開メンバ関数)
終端へのイテレータを返す
(公開メンバ関数)
要素数を返す(基盤となる(アダプトされた)範囲が sized_range を満たす場合にのみ提供)
(公開メンバ関数)
結果の approximately_sized_range の近似サイズを返す
(公開メンバ関数)
から継承された ranges::view_interface
は、派生ビューが空かどうかを返します。ただし、 sized_range または forward_range
(のパブリックメンバ関数 std::ranges::view_interface<D>)
(C++23)
は、範囲の先頭を指す定数イテレータを返します。
(のパブリックメンバ関数 std::ranges::view_interface<D>)
(C++23)
は、範囲の定数イテレータの番兵を返します。
(のパブリックメンバ関数 std::ranges::view_interface<D>)
は、派生ビューが空でないかどうかを返します。ただし、 ranges::empty が適用可能な場合に限ります。
(のパブリックメンバ関数 std::ranges::view_interface<D>)
は、派生ビューのデータのアドレスを取得します。ただし、そのイテレータ型が contiguous_iterator
(のパブリックメンバ関数 std::ranges::view_interface<D>)
は、派生ビューの最初の要素を返します。ただし、 forward_range
(のパブリックメンバ関数 std::ranges::view_interface<D>)
は、派生ビューの最後の要素を返します。ただし、 bidirectional_range と common_range
(のパブリックメンバ関数 std::ranges::view_interface<D>)
は、 n番目 の要素を返します。ただし、 random_access_range
(のパブリックメンバ関数 std::ranges::view_interface<D>)

推論ガイド

ヘルパーテンプレート

template < class T >

constexpr bool enable_borrowed_range < std :: ranges :: common_view < T >> =

ranges:: enable_borrowed_range < T > ;
(C++20以降)

この ranges::enable_borrowed_range の特殊化は、基となるビューが borrowed_range を満たす場合に、 common_view がそれを満たすようにする。

注記

common_view は、イテレータと番兵が同じ型であることを期待するレガシーアルゴリズムを扱う際に有用です。

例

#include <initializer_list>
#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); // エラー: "common range" が必要
    auto c1 = std::ranges::common_view{r1};
    std::cout << "accumulate: " << std::accumulate(c1.begin(), c1.end(), 0) << '\n';
    // 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); // エラー: "common range" が必要
    auto c2 = std::ranges::common_view{ r2 };
    std::cout << "accumulate: " << std::accumulate(c2.begin(), c2.end(), 0) << '\n';
    // ranges::view_interface から継承:
    std::cout << "c2.front(): " << c2.front() << '\n';
//  auto e3 = c2.back(); // エラー: "bidirectional range" が必要
//  auto e4 = c2.data(); // エラー: "contiguous range" が必要
//  auto e5 = c2[0];     // エラー: "random access range" が必要
}

出力例:

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 である場合、それも borrowed_range となる

関連項目

範囲が同一のイテレータ型とセンチネル型を持つことを指定する
(コンセプト)
イテレータ型とそのセンチネルを共通のイテレータ型に適合させる
(クラステンプレート)