Curious
Curious

Reputation: 21540

Do C++ standard library algorithms use std::advance

I have not been able to find a decent source for this. Do all algorithms in the <algorithm> header use std::advance to increment (and possibly decrement) iterators?

Also another related follow up - I saw the cppreference page for the RandomAccessIterator concept (http://en.cppreference.com/w/cpp/concept/RandomAccessIterator) , but it did not address the requirement for having member alias for an iterator category. If an iterator class does not have a member typedef named iterator_category that is aliased to random_access_iterator_tag but supports the operations that are mentioned in the cppreference page (http://en.cppreference.com/w/cpp/concept/RandomAccessIterator) for RandomAccessIterator, does the c++ standard library assume that the iterator is a random access iterator?

Note I meant to ask this question with reference to the C++ standard. i.e. "What does the standard say about this?"

Upvotes: 3

Views: 320

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275820

The standard does not specify the implementation of C++ std algorithms explicitly. It specifies behavior, and sometimes how many times particular operations are done. This can leave the implementer with little practical choice, but it is not explicitly specified.

std::advance is never called out in the standard as being called by another algorithm to the best of my knowledge. Which means it could be used, or not used, in a particular implementation of a particular algorithm.

The effects of std::advance are specified. Insofar as other algorithms specify their operation counts, in order to call std::advance it must not break their guarantees.

In short, using it is permitted and not required.

Upvotes: 2

Related Questions