std::ranges:: views:: as_const, std::ranges:: as_const_view
|
ヘッダーで定義
<ranges>
|
||
|
template
<
ranges::
view
V
>
requires
ranges::
input_range
<
V
>
|
(1) | (C++23以降) |
|
namespace
views
{
inline
constexpr
/* 未規定 */
as_const
=
/* 未規定 */
;
|
(2) | (C++23以降) |
|
呼び出しシグネチャ
|
||
|
template
<
ranges::
viewable_range
R
>
requires
/* 下記参照 */
|
(C++23以降) | |
T
を
decltype
(
(
e
)
)
、
U
を
std::
remove_cvref_t
<
T
>
とする。このとき式
views
::
as_const
(
e
)
は以下のいずれかと
式等価
である:
-
views::
all
(
e
)
(この式が well-formed であり、かつ
views::
all_t
<
T
>
が
constant_rangeをモデルする場合) -
それ以外の場合、
std::
span
<
const
X, Extent
>
(
e
)
(
Uが何らかの型Xと範囲Extentに対する std:: span < X, Extent > を表す場合) -
それ以外の場合、
ranges::
ref_view
(
static_cast
<
const
X
&
>
(
e.
base
(
)
)
)
(
Uが何らかの型Xに対する ranges:: ref_view < X > を表し、かつ const X がconstant_rangeをモデルする場合) -
それ以外の場合、
ranges::
ref_view
(
static_cast
<
const
U
&
>
(
e
)
)
(
eが左辺値であり、 const U がconstant_rangeをモデルし、かつUがviewをモデルしない場合) - それ以外の場合、 as_const_view { e } 。
as_const_view
は常に
constant_range
をモデル化し、基盤となるビュー
V
がそれぞれのコンセプトをモデル化する場合、
contiguous_range
、
random_access_range
、
bidirectional_range
、
forward_range
、
borrowed_range
、
common_range
、および
sized_range
もモデル化します。
目次 |
データメンバ
| メンバー | 説明 |
V
base_
(private)
|
基となるビュー
( 説明専用メンバーオブジェクト* ) |
メンバー関数
as_const_view
を構築する
(public member function) |
|
基になるビュー
V
を返す
(public member function) |
|
as_const_view
の先頭イテレータを返す
(public member function) |
|
as_const_view
の終端イテレータを返す
(public member function) |
|
|
ビューが有界の場合、そのサイズを返す
(public member function) |
|
|
(C++26)
|
基になる
approximately_sized_range
の近似サイズを返す
(public member function) |
std::ranges::view_interface から継承 |
|
派生ビューが空かどうかを返す(
sized_range
または
forward_range
を満たす場合のみ提供)
(
std::ranges::view_interface<D>
のpublic member function)
|
|
|
(C++23)
|
範囲の先頭を指す定数イテレータを返す
(
std::ranges::view_interface<D>
のpublic member function)
|
|
(C++23)
|
範囲の定数イテレータの番兵を返す
(
std::ranges::view_interface<D>
のpublic member function)
|
|
派生ビューが空でないかどうかを返す(
ranges::empty
が適用可能な場合のみ提供)
(
std::ranges::view_interface<D>
のpublic member function)
|
|
派生ビューのデータのアドレスを取得する(そのイテレータ型が
contiguous_iterator
を満たす場合のみ提供)
(
std::ranges::view_interface<D>
のpublic member function)
|
|
派生ビューの最初の要素を返す(
forward_range
を満たす場合に提供)
(
std::ranges::view_interface<D>
のpublic member function)
|
|
派生ビューの最後の要素を返す(
bidirectional_range
かつ
common_range
を満たす場合のみ提供)
(
std::ranges::view_interface<D>
のpublic member function)
|
|
派生ビューの
n
番目
の要素を返す(
random_access_range
を満たす場合のみ提供)
(
std::ranges::view_interface<D>
のpublic member function)
|
|
std::ranges::as_const_view:: as_const_view
|
as_const_view
(
)
requires
std::
default_initializable
<
V
>
=
default
;
|
(1) | (since C++23) |
|
constexpr
explicit
as_const_view
(
V base
)
;
|
(2) | (since C++23) |
パラメータ
| base | - | ビュー |
std::ranges::as_const_view:: base
|
constexpr
V base
(
)
const
&
requires
std::
copy_constructible
<
V
>
;
|
(1) | (since C++23) |
|
constexpr
V base
(
)
&&
;
|
(2) | (since C++23) |
基になるビューを返します。
base_
;
と等価です。
base_
)
;
と等価です。
std::ranges::as_const_view:: begin
|
constexpr
auto
begin
(
)
requires
(
!
/*simple_view*/
<
V
>
)
;
|
(1) | (since C++23) |
|
constexpr
auto
begin
(
)
const
requires
ranges::
range
<
const
V
>
;
|
(2) | (since C++23) |
ビューの定数イテレータを返します。
return
ranges::
cbegin
(
base_
)
;
と等価です。
std::ranges::as_const_view:: end
|
constexpr
auto
end
(
)
requires
(
!
/*simple_view*/
<
V
>
)
;
|
(1) | (since C++23) |
|
constexpr
auto
end
(
)
const
requires
ranges::
range
<
const
V
>
;
|
(2) | (since C++23) |
ビューの定数センチネルを返します。
return
ranges::
cend
(
base_
)
;
と等価です。
std::ranges::as_const_view:: size
|
constexpr
auto
size
(
)
requires
ranges::
sized_range
<
V
>
;
|
(1) | (since C++23) |
|
constexpr
auto
size
(
)
const
requires
ranges::
sized_range
<
const
V
>
;
|
(2) | (since C++23) |
ビューが有界である場合、そのサイズを返します。次と等価です:
return
ranges::
size
(
base_
)
;
。
std::ranges::as_rvalue_view:: reserve_hint
|
constexpr
auto
reserve_hint
(
)
requires ranges :: approximately_sized_range < V > ; |
(1) | (since C++26) |
|
constexpr
auto
reserve_hint
(
)
const
requires ranges :: approximately_sized_range < const V > ; |
(2) | (since C++26) |
base_
)
を返します。
推論ガイド
|
template
<
class
R
>
as_const_view ( R && ) - > as_const_view < views:: all_t < R >> ; |
(C++23以降) | |
ヘルパーテンプレート
|
template
<
class
T
>
constexpr
bool
enable_borrowed_range
<
std
::
ranges
::
as_const_view
<
T
>>
=
|
(C++23以降) | |
この
ranges::enable_borrowed_range
の特殊化は、基になるビューが
borrowed_range
を満たす場合に、
as_const_view
が
borrowed_range
を満たすようにする。
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_ranges_as_const
|
202207L
|
(C++23) |
ranges::as_const_view
,
std::
const_iterator
|
__cpp_lib_ranges_reserve_hint
|
202502L
|
(C++26) |
ranges::approximately_sized_range
および
reserve_hint
|
例
#include <cassert> #include <ranges> int main() { int x[]{1, 2, 3, 4, 5}; auto v1 = x | std::views::drop(2); assert(v1.back() == 5); v1[0]++; // OK: 非const要素は変更可能 auto v2 = x | std::views::drop(2) | std::views::as_const; assert(v2.back() == 5); // v2[0]++; // コンパイル時エラー: 読み取り専用要素は変更不可 }
関連項目
各要素を右辺値にキャストするシーケンスの
view
(クラステンプレート) (範囲アダプタオブジェクト) |
|
|
(C++20)
|
読み取り専用範囲の先頭を指すイテレータを返す
(カスタマイゼーションポイントオブジェクト) |
|
(C++20)
|
読み取り専用範囲の終端を示すセンチネルを返す
(カスタマイゼーションポイントオブジェクト) |
|
(C++17)
|
引数への
const
参照を取得する
(関数テンプレート) |
|
(C++23)
|
イテレータを定数イテレータに変換するイテレータアダプタ
(クラステンプレート) |