std::basic_const_iterator
From cppreference.com
| ヘッダ <iterator> で定義 |
||
| template< std::input_iterator Iter > class basic_const_iterator; |
(C++23から) | |
std::basic_const_iteratorは、基になるイテレータ(少なくともLegacyInputIteratorであるか、input_iteratorをモデルとする必要がある)とまったく同じように動作するイテレータアダプタですが、逆参照すると、基になるイテレータによって返される値が不変に変換されます。std::basic_const_iteratorの特殊化は定数イテレータです。つまり、要素の変更は許可されないため、イテレータを出力イテレータとして使用することはできません。
目次 |
[編集] メンバ型
| メンバ型 | 定義 |
iterator_category(条件付きで存在) |
それ以外の場合、メンバ |
iterator_concept
|
|
value_type
|
std::iter_value_t<Iter> |
difference_type
|
std::iter_difference_t<Iter> |
reference (private) |
std::iter_const_reference_t<Iter> (説明専用メンバ型*) |
[編集] メンバオブジェクト
| メンバ名 | 定義 |
current (private) |
base()がコピーまたは移動する基になるイテレータ(説明用のメンバオブジェクト*) |
[編集] メンバ関数
新しいbasic_const_iteratorを構築します。(public member function) | |
| 基底イテレータにアクセスする (public member function) | |
| 指し示す要素にアクセスする (public member function) | |
| インデックスで要素にアクセスする (public member function) | |
| イテレータを進めるまたは後退させます。 (public member function) | |
| 基になるイテレータが変換可能な任意の定数イテレータに変換します。 (public member function) | |
| 基底イテレータを比較する (public member function) |
[編集] 非メンバ関数
basic_const_iteratorと非basic_const_iteratorを比較します。(function template) | |
| (C++23) |
イテレータを進めるまたは後退させます。 (function template) |
| (C++23) |
2つのイテレータアダプタ間の距離を計算する (関数テンプレート) |
| (C++23) |
基底イテレータの間接参照の結果を関連する右辺値参照型にキャストする (関数) |
[編集] ヘルパークラス
イテレータとアダプトされたbasic_const_iterator型の共通型を決定します。(class template specialization) |
[編集] ヘルパーエイリアステンプレート
| template< std::input_iterator I > using const_iterator = /* 説明を参照 */; |
(C++23から) | |
Iがconstant-iterator(公開されていないコンセプト)をモデルとする場合、const_iterator<I>は型Iを指します。それ以外の場合、basic_const_iterator<I>を指します。
| template< std::semiregular S > using const_sentinel = /* 説明を参照 */; |
(C++23から) | |
Sがinput_iteratorをモデルとする場合、const_sentinel<S>は型const_iterator<S>を指します。それ以外の場合、Sを指します。
[編集] ヘルパー関数テンプレート
| template< std::input_iterator T > constexpr const_iterator<T> make_const_iterator( I it ) { return it; } |
(C++23から) | |
| template< std::semiregular S > constexpr const_sentinel<S> make_const_sentinel( S s ) { return s; } |
(C++23から) | |
[編集] 注記
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_ranges_as_const |
202207L |
(C++23) | std::basic_const_iterator
|
202311L |
(C++23) (DR) |
std::basic_const_iteratorは、基になる型の変換可能性に従う必要があります。 |
[編集] 例
このコードを実行
#include <cassert> #include <iterator> #include <vector> int main() { std::vector v{1, 2, 3}; std::vector<int>::iterator i = v.begin(); *i = 4; // OK, v[0] == 4 now i[1] = 4; // OK, the same as *(i + 1) = 4; auto ci = std::make_const_iterator(i); assert(*ci == 4); // OK, can read the underlying object assert(ci[0] == 4); // OK, ditto // *ci = 13; // Error: location is read-only // ci[0] = 13; // Error: ditto ci.base()[0] = 42; // OK, underlying iterator is writable assert(*ci == 42); // OK, underlying location v[0] was modified }
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| P2836R1 | C++23 | basic_const_iteratorは基になる型の変換可能性に従っていません。 |
変換演算子が提供されています。 |