[range.iter.ops.general] (original) (raw)

23 Iterators library [iterators]

23.4 Iterator primitives [iterator.primitives]

23.4.4 Range iterator operations [range.iter.ops]

23.4.4.1 General [range.iter.ops.general]

The library includes the function templatesranges​::​advance, ranges​::​distance,ranges​::​next, and ranges​::​prevto manipulate iterators.

These operations adapt to the set of operators provided by each iterator category to provide the most efficient implementation possible for a concrete iterator type.

[Example 1:

ranges​::​advance uses the + operator to move arandom_­access_­iterator forward n steps in constant time.

For an iterator type that does not model random_­access_­iterator,ranges​::​advance instead performs n individual increments with the ++ operator.

— _end example_]

[Example 2: void foo() { using namespace std::ranges; std::vector<int> vec{1,2,3}; distance(begin(vec), end(vec)); }

The function call expression at #1 invokes std​::​ranges​::​distance, not std​::​distance, despite that (a) the iterator type returned from begin(vec) and end(vec)may be associated with namespace std and (b) std​::​distance is more specialized ([temp.func.order]) thanstd​::​ranges​::​distance since the former requires its first two parameters to have the same type.

— _end example_]

The number and order of deducible template parameters for the function templates defined in [range.iter.ops] is unspecified, except where explicitly stated otherwise.