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

從 cppreference.com
 
 
範圍庫
範圍適配器
 
 
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::basic_istream_view
  • std::ranges::filter_view
  • std::ranges::join_view
  • std::ranges::lazy_split_view
  • std::ranges::split_view

繼承的 operator[] 成員函數對 std::ranges::empty_view 可用,但對它的調用始終導致未定義行為。

[編輯] 示例