A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../error/error_code/../../iterator/const_sentinel.html below:

std::basic_const_iterator - cppreference.com

std::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.

[edit] Member types [edit] Member objects Member name Definition current (private) the underlying iterator from which base() copies or moves
(exposition-only member object*) [edit] Member functions [edit] Non-member functions [edit] Helper classes [edit] Helper alias templates

If I models constant-iterator (an exposition-only concept), then const_iterator<I> denotes a type I. Otherwise, basic_const_iterator<I>.

(since C++23)

If S models input_iterator, then const_sentinel<S> denotes a type const_iterator<S>. Otherwise, S.

[edit] Helper function templates template< std::input_iterator T >
constexpr const_iterator<T> make_const_iterator( I it ) { return it; }
(since C++23) template< std::semiregular S >
constexpr const_sentinel<S> make_const_sentinel( S s ) { return s; }
(since C++23) [edit] Notes [edit] Example
#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
}
[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior P2836R1 C++23 basic_const_iterator doesn't follow its underlying type's convertibility conversion operator provided

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4