From cppreference.com
template<std::input_iteratorIter>classbasic_const_iterator;(since C++23)std::basic_const_iterator is an iterator adaptor which behaves exactly like the underlying iterator (which must be at least an
or model
), except that dereferencing converts the value returned by the underlying iterator as immutable. Specializations of std::basic_const_iterator are constant iterators, that is, the iterator can never be used as an output iterator because modifying elements is not allowed.
Member types
Member type Definition iterator_category
(conditionally present)If Iter models
:
member iterator_category is the same type as std::iterator_traits<Iter>::iterator_category.
Otherwise, there is no member iterator_category.
iterator_concept
, if Iter models
;
std::random_access_iterator_tag
, if Iter models
;
std::bidirectional_iterator_tag
, if Iter models
;
, if Iter models
;
otherwise.
value_typestd::iter_value_t<Iter>difference_typestd::iter_difference_t<Iter>reference(private)std::iter_const_reference_t<Iter>
(exposition-only member type*)Member objects
Member name Definition current(private)the underlying iterator from which
copies or moves
(exposition-only member object*)Member functions
constructs a new basic_const_iterator
(public member function)
accesses the underlying iterator
(public member function)
accesses the pointed-to element
(public member function)
accesses an element by index
(public member function)
operator++operator++(int)operator+=operator--operator--(int)operator-=
advances or decrements the iterator
(public member function)
converts into any constant iterator to which an underlying iterator can be convertible
(public member function)
operator==operator<operator>operator<=operator>=operator<=>
compares the underlying iterators
(public member function)
Non-member functions
Helper classes
Helper alias templates
template<std::input_iteratorI>usingconst_iterator=/* see description */;(since C++23)If I models
(an exposition-only concept), then const_iterator<I> denotes a type I. Otherwise, basic_const_iterator<I>.
template<std::semiregularS>usingconst_sentinel=/* see description */;(since C++23)If S models
, then const_sentinel<S> denotes a type const_iterator<S>. Otherwise, S.
Helper function templates
template<std::input_iteratorT>constexprconst_iterator<T>make_const_iterator(Iit){returnit;}(since C++23)template<std::semiregularS>constexprconst_sentinel<S>make_const_sentinel(Ss){returns;}(since C++23)Notes
macro ValueStdFeature
(C++23)std::basic_const_iterator
(C++23)
(DR)std::basic_const_iterator should follow its underlying type's convertibility Example
Run this code
#include<cassert>#include<iterator>#include<vector>intmain(){std::vectorv{1,2,3};std::vector<int>::iteratori=v.begin();*i=4;// OK, v[0] == 4 nowi[1]=4;// OK, the same as *(i + 1) = 4;autoci=std::make_const_iterator(i);assert(*ci==4);// OK, can read the underlying objectassert(ci[0]==4);// OK, ditto// *ci = 13; // Error: location is read-only// ci[0] = 13; // Error: dittoci.base()[0]=42;// OK, underlying iterator is writableassert(*ci==42);// OK, underlying location v[0] was modified}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++23 basic_const_iterator doesn't follow its underlying type's convertibility conversion operator provided