std::basic_const_iterator(3)
π std::basic_const_iterator π
okay bestie, welcome to the <iterator> page for std::basic_const_iterator, aka std::basic_const_iterator(3) in the C++ Standard Libary manual. this one is literally an icon π
β¨ NAME β¨
std::basic_const_iteratorβstd::basic_const_iterator(yes thatβs the whole name, main character energy only)
πΈ Synopsis πΈ
Defined in header <iterator>.
template< std::input_iterator Iter > (since C++23)
class basic_const_iterator;
std::basic_const_iterator is an iterator adaptor which behaves exactly like the underlying iterator (which must be at least an LegacyInputIterator or model input_iterator), 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 π
iterator_category- If
Itermodelsforward_iterator: memberiterator_categoryis the same type asstd::iterator_traits<Iter>::iterator_category. - Otherwise, there is no member
iterator_category.
- If
iterator_conceptstd::contiguous_iterator_tag, ifItermodelscontiguous_iterator;std::random_access_iterator_tag, ifItermodelsrandom_access_iterator;std::bidirectional_iterator_tag, ifItermodelsbidirectional_iterator;std::forward_iterator_tag, ifItermodelsforward_iterator;std::input_iterator_tagotherwise.
value_typeβstd::iter_value_t<Iter>difference_typeβstd::iter_difference_t<Iter>reference(private) βstd::iter_const_reference_t<Iter>(exposition-only member type*)
π¦ Member objects π¦
current(private) β the underlying iterator from whichbase()copies or moves (exposition-only member object*)
π Member functions π
constructorβ constructs a new iterator adaptor (public member function)baseβ accesses the underlying iterator (public member function)operator*β accesses the pointed-to element (public member function)operator->β accesses the pointed-to element (public member function)operator[]β accesses an element by index (public member function)operator++β advances or decrements the iterator (public member function)operator++(int)β advances or decrements the iterator (public member function)operator+=β advances or decrements the iterator (public member function)operator--β advances or decrements the iterator (public member function)operator--(int)β advances or decrements the iterator (public member function)operator-=β advances or decrements the iterator (public member function)operator constant-iteratorβ converts into any constant iterator to which an underlying iterator can be convertible (public member function)operator==β compares the underlying iterators (public member function)operator<β compares the underlying iterators (public member function)operator>β compares the underlying iterators (public member function)operator<=β compares the underlying iterators (public member function)operator>=β compares the underlying iterators (public member function)operator<=>β compares the underlying iterators (public member function)
π§ Non-member functions π§
operator<,operator>,operator<=,operator>=,operator<=>β comparesbasic_const_iteratorwith non-basic_const_iterator(function template)(C++23)operator+,operator-β advances or decrements the iterator (function template)(C++23)operator-β computes the distance between two iterator adaptors (function template)(C++23)iter_moveβ casts the result of dereferencing the underlying iterator to its associated rvalue reference type (function)(C++23)
π Helper classes π
std::common_type<std::basic_const_iterator>β determines the common type of an iterator and an adaptedbasic_const_iteratortype (class template specialization)(C++23)
π Helper alias templates π
template< std::input_iterator I > (since C++23)
using const_iterator = /* see description */;
If I models constant-iterator (an exposition-only concept), then const_iterator<I> denotes a type I. Otherwise, basic_const_iterator<I>.
template< std::semiregular S > (since C++23)
using const_sentinel = /* see description */;
If S models input_iterator, then const_sentinel<S> denotes a type const_iterator<S>. Otherwise, S.
β Helper function templates β
template< std::input_iterator T > (since C++23)
constexpr const_iterator<T> make_const_iterator( I it ) { return it; }
template< std::semiregular S > (since C++23)
constexpr const_sentinel<S> make_const_sentinel( S s ) { return s; }
π₯ Notes π₯
__cpp_lib_ranges_as_constβ value202207L, std(C++23), featurestd::basic_const_iterator__cpp_lib_ranges_as_constβ value202311L, std(C++23)(DR), featurestd::basic_const_iterator should follow its underlying type's convertibility
π Example π
// Run this code
#include <cassert>
#include <iterator>
#include <vector>
int main()
{
std::vector v{1, 2, 3};
std::vector<int>::iterator i = v.begin();
*i = 4; // OK, v[0] == 4 now
i[1] = 4; // OK, the same as *(i + 1) = 4;
auto ci = std::make_const_iterator(i);
assert(*ci == 4); // OK, can read the underlying object
assert(ci[0] == 4); // OK, ditto
// *ci = 13; // Error: location is read-only
// ci[0] = 13; // Error: ditto
ci.base()[0] = 42; // OK, underlying iterator is writable
assert(*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.
P2836R1β applied toC++23; behavior as published:basic_const_iterator doesn't follow its underlying type's convertibility; correct behavior:conversion operator provided.
π source/version/date moment: this oneβs from http://cppreference.com, dated 2024.06.10, and itβs the std::basic_const_iterator(3) page β mwah, periodt. π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms