constexpr decltype(auto) operator*(); | (1) | (since C++20) |
constexpr decltype(auto) operator*() const
requires /*dereferenceable*/<const I>;
| (2) | (since C++20) |
constexpr auto operator->() const
requires /* see description */;
| (3) | (since C++20) |
| Helper types | ||
class /*proxy*/ { // exposition only
std::iter_value_t<I> keep_;
constexpr proxy(std::iter_reference_t<I>&& x)
: keep_(std::move(x)) {}
public:
constexpr const std::iter_value_t<I>* operator->() const noexcept {
return std::addressof(keep_);
}
};
| (4) | (since C++20) |
Returns pointer or reference to the current element, or a proxy holding it.
The behavior is undefined if the underlying std::variant member object var does not hold an object of type I, i.e. std::holds_alternative<I>(var) is equal to false.
Let it denote the iterator of type I held by var, that is std::get<I>(var).
it.return it;, if I is a pointer type or if the expression it.operator->() is well-formed auto&& tmp = *it; return std::addressof(tmp);, if std::iter_reference_t<I> is a reference type, return proxy(*it);, where proxy is an exposition only class (4).requires-clause is equivalent tostd::indirectly_readable<const I> && (
requires(const I& i) { i.operator->(); } ||
std::is_reference_v<std::iter_reference_t<I>> ||
std::constructible_from<std::iter_value_t<I>, std::iter_reference_t<I>>
).(none).
*it.#include <algorithm>
#include <complex>
#include <iostream>
#include <iterator>
#include <initializer_list>
using std::complex_literals::operator""i;
int main() {
const auto il = {1i,3.14+2i,3i,4i,5i};
using CI = std::common_iterator<
std::counted_iterator<decltype(il)::iterator>,
std::default_sentinel_t>;
CI ci { std::counted_iterator{
std::next(begin(il), 1), ssize(il) - 1} };
std::cout << *ci << ' ' << ci->real() << '\n';
}Output:
(3.14,2) 3.14
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 3574 | C++20 | variant was fully constexpr (P2231R1) but common_iterator was not | also made constexpr |
| LWG 3595 | C++20 | functions of the proxy type lacked constexpr and noexcept | added |
| LWG 3672 | C++20 | operator-> might return by reference in usual cases | always returns by value |
|
(C++20) | constructs a new iterator adaptor (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/iterator/common_iterator/operator*