More on Uses-cases for Concepts

Discover further use-cases of concepts in C++20.

Overloading

std::advance is an algorithm of the Standard Template Library. It increments a given iterator iter by n elements. Based on the capabilities of the given iterator, a different advanced strategy could be used. For example, a std::forward_list supports an iterator that can only advance in one direction, while a std::list supports a bidirectional iterator, and a std::vector supports a random access iterator.

Consequently, for an iterator provided by a std::forward_list or std::list, a call to std::advance(iter, n) has to be incremented n times (see the structure of a std::list). This time complexity does not hold for an std::random_access_iterator provided by an std::vector. The number n can just be added to the iterator. A linear time complexity O(n) becomes, therefore, a constant complexity O(1). Concepts can be used to distinguish iterator types.

The following program should give you a general idea.

Get hands-on with 1200+ tech skills courses.