std::common_iterator<I,S>::operator++ - cppreference.com
来自cppreference.com
constexpr common_iterator& operator++(); |
(1) | (C++20 起) |
constexpr decltype(auto) operator++( int ); |
(2) | (C++20 起) |
| 辅助类型 |
||
class /*postfix_proxy*/ { std::iter_value_t<I> keep_; constexpr postfix_proxy(std::iter_reference_t<I>&& x) : keep_(std::forward<std::iter_reference_t<I>>(x)) {} public: constexpr const std::iter_value_t<I>& operator*() const noexcept { return 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'; }
输出:
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| P2259R1 | C++20 | 后自增在更多场合舍弃其结果 | 用代理类保有结果 |
| LWG 3546 | C++20 | 代理对象的初始化有时非良构 | 调整了场合与定义 |
| LWG 3574 | C++20 | variant 为完全 constexpr (P2231R1) 但 common_iterator 不是
|
亦使之为 constexpr |
| LWG 3595 | C++20 | 代理类型的函数缺少 constexpr 与 noexcept | 已添加 |