std::common_iterator<I,S>::operator++
來自 cppreference.com
< cpp | 迭代器 | common_iterator
constexpr common_iterator& operator++(); |
(1) | (C++20 起) |
constexpr decltype(auto) operator++( int ); |
(2) | (C++20 起) |
輔助型別 |
||
class /*postfix_proxy*/ { std::iter_value_t<I> keep_; |
(3) | (僅作說明*) |
遞增底層迭代器。
如果底層 std::variant 成員物件 var
不持有型別為 I
的物件,即 std::holds_alternative<I>(var) 等於 false,則行為未定義。
令 it
表示 var
所持有的型別為 I
的迭代器,即 std::get<I>(var)。
1) 前置遞增一。等價於 ++it; return *this;。
2) 後置遞增一。
- 如果 I 建模
forward_iterator
,則等價於:auto tmp = *this; ++*this; return tmp;。 - 如果變數定義 auto&& ref = *it++; 格式良好,或者
- std::indirectly_readable<I> 或者
- std::constructible_from<std::iter_value_t<I>, std::iter_reference_t<I>> 或者
- std::move_constructible<std::iter_value_t<I>>
- 為 false,則等價於:return it++;。
- 否則,等價於:postfix_proxy p(**this); ++*this; return p;,其中
postfix_proxy
是一個僅用於說明的輔助型別 (3)。
目錄 |
[編輯] 引數
(無)
[編輯] 返回值
1) *this
2) 更改前
*this
的副本,或底層迭代器後置遞增的結果,或如上所述保留當前元素值的代理。[編輯] 示例
執行此程式碼
#include <algorithm> #include <initializer_list> #include <iostream> #include <iterator> int main() { const auto il = {1, 2, 3, 4, 5, 6}; using CI = std::common_iterator< std::counted_iterator<std::initializer_list<int>::iterator>, std::default_sentinel_t >; CI first{std::counted_iterator{std::begin(il), std::ssize(il) - 2}}; for (; first != std::default_sentinel; ++first) std::cout << *first << ' '; std::cout << '\n'; }
輸出
1 2 3 4
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
P2259R1 | C++20 | 後置遞增可能在更多情況下丟棄其結果 | 使用代理類來保留結果 |
LWG 3546 | C++20 | 代理物件的初始化有時格式錯誤 | 情況和定義已調整 |
LWG 3574 | C++20 | variant 完全為 constexpr (P2231R1) 但 common_iterator 不是 |
也設為 constexpr |
LWG 3595 | C++20 | 代理型別的函式缺少 constexpr 和 noexcept | 已新增 |
[編輯] 參閱
計算兩個迭代器介面卡之間的距離 (函式模板) |