名稱空間
變體
操作

std::ranges::view_interface<D>::operator[]

來自 cppreference.com
 
 
範圍庫 (Ranges library)
範圍介面卡 (Range adaptors)
 
 
template<ranges::random_access_range R = D>
constexpr decltype(auto) operator[]( ranges::range_difference_t<R> n );
(1) (C++20 起)
template<ranges::random_access_range R = const D>
constexpr decltype(auto) operator[]( ranges::range_difference_t<R> n ) const;
(2) (C++20 起)

operator[] 成員函式的預設實現透過起始迭代器相對於指定偏移量獲取元素,複用迭代器型別的 operator[]

1)derivedstatic_cast<D&>(*this)。等價於 return ranges::begin(derived)[n];
2)(1),但 derivedstatic_cast<const D&>(*this)

目錄

[編輯] 引數

n - 要返回元素的下標

[編輯] 返回值

相對於起始迭代器偏移量 n 處的元素。

[編輯] 注意

在 C++20 中,標準庫中沒有派生自 std::ranges::view_interface 的型別提供它們自己的 operator[] 成員函式。

然而,以下派生型別不能使用預設實現,因為它們從不滿足 random_access_range

std::ranges::empty_view 可用繼承的 operator[] 成員函式,但呼叫它總是導致未定義行為。

[編輯] 示例