This pattern is licensed under the CC0 Public Domain Dedication.
Implement algorithms that can be applied to any generic range of elements.
Note: The existing algorithms in the standard library are not range-based but iterator-based. However, the Ranges Technical Specification is experimenting with introducing ranges and range-based algorithms to the standard, which are more flexible and provide a simpler interface to client code.
Lines 4–16 define a function template representing a range-based
algorithm. It takes a single range argument, which is
any type that supports
end functions that provide
iterators to the beginning and end of the range. A range may
be classified depending on the iterators that it provides:
- Forward Range - provides Forward Iterators
- Bidirectional Range - provides Bidirectional Iterators
- Random Access Range - provides Random Access Iterators
For the example code, we assume that the algorithm requires only Forward
Iterators, so can be applied to any Forward Range. We therefore
name the template parameter
ForwardRange on line 4 to illustrate
On lines 12–13, we call
end on the range to get the
respective iterators to the beginning and end of the range.
We use using-declarations on lines 7–8 to
allow these calls to be found via argument-dependent
before using the standard
std::end functions. With these iterators, we
can now implement the algorithm over the elements between them.
If the iterators are not necessary to implement the algorithm, we
may instead be able to use a simple
27 August 2018