From cppreference.com
constexprdecltype(auto)operator*(); (1) (since C++20)constexprdecltype(auto)operator*()constrequires/*dereferenceable*/<constI>; (2) (since C++20)constexprautooperator->()constrequires/* see description */; (3) (since C++20)Helper types
class/*proxy*/{std::iter_value_t<I>keep_;constexprproxy(std::iter_reference_t<I>&&x):keep_(std::move(x)){}public:constexprconststd::iter_value_t<I>*operator->()constnoexcept{returnstd::addressof(keep_);}}; (4) (exposition only*)Returns pointer or reference to the current element, or a proxy holding it.
Let it denote the iterator of type I held by var, that is std::get<I>(var).
1,2) Returns the result of dereferencing it.
3) Returns a pointer or underlying iterator to the current element, or a proxy holding it:
Equivalent to returnit;, if I is a pointer type or if the expression it.operator->() is well-formed.
Otherwise, equivalent to auto&&tmp=*it;returnstd::addressof(tmp);, if std::iter_reference_t<I> is a reference type.
Otherwise, equivalent to return/*proxy*/(*it);.
The expression in the requires-clause is equivalent to std::indirectly_readable<constI>&&(requires(constI&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>>).
If std::holds_alternative<I>(var) is false, the behavior is undefined.
(until C++26)If std::holds_alternative<I>(var) is false:
If the implementation is
, a
occurs.
If the implementation is not hardened, the behavior is undefined.
(since C++26)Return value
1,2) Reference to the current element, or prvalue temporary. Equivalent to *it.
3) Pointer or iterator to the current element or proxy holding it as described above.
Example
Run this code
#include<complex>#include<initializer_list>#include<iostream>#include<iterator>usingstd::complex_literals::operator""i;intmain(){constautoil={1i,3.14+2i,3i,4i,5i};usingCI=std::common_iterator<std::counted_iterator<decltype(il)::iterator>,std::default_sentinel_t>;CIci{std::counted_iterator{std::next(begin(il),1),std::ssize(il)-1}};std::cout<<*ci<<' '<<ci->real()<<'\n';}Output:
(3.14,2) 3.14 Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++20 variant was fully constexpr (P2231R1) but common_iterator was not also made constexpr
C++20 functions of the proxy type lacked constexpr and noexcept added
C++20 operator-> might return by reference in usual cases always returns by value See also